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: UiConstants.radiusLg,
        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.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Card content starts directly as per prototype

                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Logo
                    Container(
                      width: 44,
                      height: 44,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [
                            UiColors.primary.withOpacity(0.09),
                            UiColors.primary.withOpacity(0.03),
                          ],
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                        ),
                        borderRadius: BorderRadius.circular(12),
                        border: Border.all(
                          color: UiColors.primary.withOpacity(0.09),
                        ),
                      ),
                      child: shift.logoUrl != null
                          ? ClipRRect(
                              borderRadius: BorderRadius.circular(12),
                              child: Image.network(
                                shift.logoUrl!,
                                fit: BoxFit.contain,
                              ),
                            )
                          : const Center(
                              child: Icon(
                                UiIcons.briefcase,
                                color: UiColors.primary,
                                size: 20,
                              ),
                            ),
                    ),
                    const SizedBox(width: 12),

                    // Details
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      shift.title,
                                      style: UiTypography.body2m.copyWith(
                                        color: UiColors.textPrimary,
                                      ),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    Text(
                                      shift.clientName,
                                      style: UiTypography.body3r.copyWith(
                                        color: UiColors.textSecondary,
                                      ),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ],
                                ),
                              ),
                              const SizedBox(width: 8),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Text(
                                    "\$${totalPay.toStringAsFixed(0)}",
                                    style: UiTypography.title1m.copyWith(
                                      color: UiColors.textPrimary,
                                    ),
                                  ),
                                  Text(
                                    "\$${shift.hourlyRate.toInt()}/hr · ${hours.toInt()}h",
                                    style: UiTypography.footnote2r.copyWith(
                                      color: UiColors.textSecondary,
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                          const SizedBox(height: 12),

                          // Date & Time
                          Row(
                            children: [
                              const Icon(
                                UiIcons.calendar,
                                size: 12,
                                color: UiColors.iconSecondary,
                              ),
                              const SizedBox(width: 4),
                              Text(
                                _formatDate(shift.date),
                                style: UiTypography.footnote1r.copyWith(
                                  color: UiColors.textSecondary,
                                ),
                              ),
                              const SizedBox(width: 12),
                              const Icon(
                                UiIcons.clock,
                                size: 12,
                                color: UiColors.iconSecondary,
                              ),
                              const SizedBox(width: 4),
                              Text(
                                "${_formatTime(shift.startTime)} - ${_formatTime(shift.endTime)}",
                                style: UiTypography.footnote1r.copyWith(
                                  color: UiColors.textSecondary,
                                ),
                              ),
                            ],
                          ),
                          const SizedBox(height: 4),

                          // Location
                          Row(
                            children: [
                              const Icon(
                                UiIcons.mapPin,
                                size: 12,
                                color: UiColors.iconSecondary,
                              ),
                              const SizedBox(width: 4),
                              Expanded(
                                child: Text(
                                  shift.locationAddress.isNotEmpty
                                      ? shift.locationAddress
                                      : shift.location,
                                  style: UiTypography.footnote1r.copyWith(
                                    color: UiColors.textSecondary,
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),

          Padding(
            padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
            child: Row(
              children: [
                Expanded(
                  child: OutlinedButton(
                    onPressed: onDecline,
                    style: OutlinedButton.styleFrom(
                      foregroundColor: UiColors.iconSecondary,
                      side: const BorderSide(color: UiColors.border),
                      padding: const EdgeInsets.symmetric(vertical: 14),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: Text(t.staff_shifts.action.decline),
                  ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: ElevatedButton(
                    onPressed: onConfirm,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: UiColors.primary,
                      foregroundColor: Colors.white,
                      elevation: 0,
                      padding: const EdgeInsets.symmetric(vertical: 14),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: Text(t.staff_shifts.action.confirm),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
