import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:design_system/design_system.dart';
import 'package:core_localization/core_localization.dart';
import '../../domain/entities/time_card.dart';

/// A card widget displaying details of a single shift/timecard.
class TimesheetCard extends StatelessWidget {
  final TimeCard timesheet;

  const TimesheetCard({super.key, required this.timesheet});

  @override
  Widget build(BuildContext context) {
    final status = timesheet.status;
    Color statusBg;
    Color statusColor;
    String statusText;

    switch (status) {
      case TimeCardStatus.approved:
        statusBg = UiColors.textSuccess.withOpacity(0.12);
        statusColor = UiColors.textSuccess;
        statusText = t.staff_time_card.status.approved;
        break;
      case TimeCardStatus.disputed:
        statusBg = UiColors.destructive.withOpacity(0.12);
        statusColor = UiColors.destructive;
        statusText = t.staff_time_card.status.disputed;
        break;
      case TimeCardStatus.paid:
        statusBg = UiColors.primary.withOpacity(0.12);
        statusColor = UiColors.primary;
        statusText = t.staff_time_card.status.paid;
        break;
      case TimeCardStatus.pending:
        statusBg = UiColors.textWarning.withOpacity(0.12);
        statusColor = UiColors.textWarning;
        statusText = t.staff_time_card.status.pending;
        break;
    }

    final dateStr = DateFormat('EEE, MMM d').format(timesheet.date);

    return Container(
      margin: const EdgeInsets.only(bottom: UiConstants.space3),
      padding: const EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.bgPopup,
        borderRadius: UiConstants.radiusLg,
        border: Border.all(color: UiColors.border),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    timesheet.shiftTitle,
                    style: UiTypography.body1m.copyWith(
                      color: UiColors.textPrimary,
                    ),
                  ),
                  Text(
                    timesheet.clientName,
                    style: UiTypography.body2r.copyWith(
                      color: UiColors.textSecondary,
                    ),
                  ),
                ],
              ),
              Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: UiConstants.space2,
                  vertical: UiConstants.space1,
                ),
                decoration: BoxDecoration(
                  color: statusBg,
                  borderRadius: UiConstants.radiusFull,
                ),
                child: Text(
                  statusText,
                  style: UiTypography.titleUppercase4b.copyWith(
                    color: statusColor,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: UiConstants.space3),
          Wrap(
            spacing: UiConstants.space3,
            runSpacing: UiConstants.space1,
            children: [
              _IconText(icon: UiIcons.calendar, text: dateStr),
              _IconText(
                icon: UiIcons.clock,
                text: '${_formatTime(timesheet.startTime)} - ${_formatTime(timesheet.endTime)}',
              ),
              if (timesheet.location != null)
                _IconText(icon: UiIcons.mapPin, text: timesheet.location!),
            ],
          ),
          const SizedBox(height: UiConstants.space3),
          Container(
            padding: const EdgeInsets.only(top: UiConstants.space3),
            decoration: const BoxDecoration(
              border: Border(top: BorderSide(color: UiColors.border)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '${timesheet.totalHours.toStringAsFixed(1)} ${t.staff_time_card.hours} @ \$${timesheet.hourlyRate.toStringAsFixed(2)}${t.staff_time_card.per_hr}',
                  style: UiTypography.body2r.copyWith(color: UiColors.textSecondary),
                ),
                Text(
                  '\$${timesheet.totalPay.toStringAsFixed(2)}',
                  style: UiTypography.title2b.copyWith(
                    color: UiColors.primary,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // Helper to safely format time strings like "HH:mm"
  String _formatTime(String t) {
    if (t.isEmpty) return '--:--';
    try {
      final parts = t.split(':');
      if (parts.length >= 2) {
        final dt = DateTime(2000, 1, 1, int.parse(parts[0]), int.parse(parts[1]));
        return DateFormat('h:mm a').format(dt);
      }
      return t;
    } catch (_) {
      return t;
    }
  }
}

class _IconText extends StatelessWidget {
  final IconData icon;
  final String text;

  const _IconText({required this.icon, required this.text});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 14, color: UiColors.iconSecondary),
        const SizedBox(width: UiConstants.space1),
        Text(
          text,
          style: UiTypography.body2r.copyWith(color: UiColors.textSecondary),
        ),
      ],
    );
  }
}
