import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:design_system/design_system.dart';
import 'package:core_localization/core_localization.dart';

class ShiftAssignmentCard extends StatelessWidget {
  final Shift shift;
  final VoidCallback onConfirm;
  final VoidCallback onDecline;
  final bool isConfirming;

  const ShiftAssignmentCard({
    super.key,
    required this.shift,
    required this.onConfirm,
    required this.onDecline,
    this.isConfirming = false,
  });

  String _formatTime(String time) {
    if (time.isEmpty) return '';
    try {
      final parts = time.split(':');
      final hour = int.parse(parts[0]);
      final minute = int.parse(parts[1]);
      final dt = DateTime(2022, 1, 1, hour, minute);
      return DateFormat('h:mm a').format(dt);
    } catch (e) {
      return time;
    }
  }

  String _formatDate(String dateStr) {
    if (dateStr.isEmpty) return '';
    try {
      final date = DateTime.parse(dateStr);
      final now = DateTime.now();
      final today = DateTime(now.year, now.month, now.day);
      final tomorrow = today.add(const Duration(days: 1));
      final d = DateTime(date.year, date.month, date.day);

      if (d == today) return 'Today';
      if (d == tomorrow) return 'Tomorrow';
      return DateFormat('EEE, MMM d').format(date);
    } catch (e) {
      return dateStr;
    }
  }

  double _calculateHours(String start, String end) {
    if (start.isEmpty || end.isEmpty) return 0;
    try {
      final s = start.split(':').map(int.parse).toList();
      final e = end.split(':').map(int.parse).toList();
      return ((e[0] * 60 + e[1]) - (s[0] * 60 + s[1])) / 60;
    } catch (_) {
      return 0;
    }
  }

  @override
  Widget build(BuildContext context) {
    final hours = _calculateHours(shift.startTime, shift.endTime);
    final totalPay = shift.hourlyRate * hours;

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: UiColors.border),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 2,
            offset: const Offset(0, 1),
          ),
        ],
      ),
      child: Column(
        children: [
          // Header
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 16, 16, 12),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Container(
                      width: 36,
                      height: 36,
                      decoration: BoxDecoration(
                        color: UiColors.secondary,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Text(
                          shift.clientName.isNotEmpty
                              ? shift.clientName[0]
                              : 'K',
                          style: UiTypography.body2b.copyWith(
                            color: UiColors.textSecondary,
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(width: 12),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          shift.title,
                          style: UiTypography.body2b.copyWith(
                            color: UiColors.textPrimary,
                          ),
                        ),
                        Text(
                          shift.clientName,
                          style: UiTypography.display3r.copyWith(
                            color: UiColors.textSecondary,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      "\$${totalPay.toStringAsFixed(0)}",
                      style: UiTypography.display2m.copyWith(
                         color: UiColors.textPrimary,
                      ),
                    ),
                    Text(
                      "\$${shift.hourlyRate}/hr · ${hours}h",
                      style: UiTypography.display3r.copyWith(
                         color: UiColors.textSecondary,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),

          // Details
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    const Icon(
                      UiIcons.calendar,
                      size: 14,
                      color: UiColors.iconSecondary,
                    ),
                    const SizedBox(width: 6),
                    Text(
                      _formatDate(shift.date),
                      style: UiTypography.display3r.copyWith(
                         color: UiColors.textSecondary,
                      ),
                    ),
                    const SizedBox(width: 16),
                    const Icon(
                      UiIcons.clock,
                      size: 14,
                      color: UiColors.iconSecondary,
                    ),
                    const SizedBox(width: 6),
                    Text(
                      "${_formatTime(shift.startTime)} - ${_formatTime(shift.endTime)}",
                      style: UiTypography.display3r.copyWith(
                         color: UiColors.textSecondary,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    const Icon(
                      UiIcons.mapPin,
                      size: 14,
                      color: UiColors.iconSecondary,
                    ),
                    const SizedBox(width: 6),
                    Expanded(
                        child: Text(
                        shift.location,
                        style: UiTypography.display3r.copyWith(
                           color: UiColors.textSecondary,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        ),
                    ),
                    ],
                ),
              ],
            ),
          ),
          
          if (isConfirming) ...[
            const Divider(height: 1),
            Row(
              children: [
                Expanded(
                  child: TextButton(
                    onPressed: onDecline,
                    style: TextButton.styleFrom(
                      foregroundColor: UiColors.destructive,
                      padding: const EdgeInsets.symmetric(vertical: 16),
                    ),
                    child: Text(t.staff_shifts.action.decline),
                  ),
                ),
                Container(width: 1, height: 48, color: UiColors.border),
                Expanded(
                  child: TextButton(
                    onPressed: onConfirm,
                    style: TextButton.styleFrom(
                      foregroundColor: UiColors.primary,
                      padding: const EdgeInsets.symmetric(vertical: 16),
                    ),
                    child: Text(t.staff_shifts.action.confirm),
                  ),
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }
}
