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

/// A section displaying the date and the shift's start/end times.
class ShiftDateTimeSection extends StatelessWidget {
  /// The ISO string of the date.
  final String date;
  
  /// The start time string (HH:mm).
  final String startTime;
  
  /// The end time string (HH:mm).
  final String endTime;
  
  /// Localization string for shift date.
  final String shiftDateLabel;
  
  /// Localization string for clock in time.
  final String clockInLabel;
  
  /// Localization string for clock out time.
  final String clockOutLabel;

  /// Creates a [ShiftDateTimeSection].
  const ShiftDateTimeSection({
    super.key,
    required this.date,
    required this.startTime,
    required this.endTime,
    required this.shiftDateLabel,
    required this.clockInLabel,
    required this.clockOutLabel,
  });

  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);
      return DateFormat('EEEE, MMMM d, y').format(date);
    } catch (e) {
      return dateStr;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            shiftDateLabel,
            style: UiTypography.titleUppercase4b.textSecondary,
          ),
          const SizedBox(height: UiConstants.space2),
          Row(
            children: [
              const Icon(
                UiIcons.calendar,
                size: 20,
                color: UiColors.primary,
              ),
              const SizedBox(width: UiConstants.space2),
              Text(
                _formatDate(date),
                style: UiTypography.headline5m.textPrimary,
              ),
            ],
          ),
          const SizedBox(height: UiConstants.space4),
          Row(
            children: [
              Expanded(
                child: _buildTimeBox(
                  clockInLabel,
                  startTime,
                ),
              ),
              const SizedBox(width: UiConstants.space4),
              Expanded(
                child: _buildTimeBox(
                  clockOutLabel,
                  endTime,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildTimeBox(String label, String time) {
    return Container(
      padding: const EdgeInsets.all(UiConstants.space3),
      decoration: BoxDecoration(
        color: UiColors.bgThird,
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
      ),
      child: Column(
        children: [
          Text(
            label,
            style: UiTypography.footnote2b.copyWith(
              color: UiColors.textSecondary,
              letterSpacing: 0.5,
            ),
          ),
          const SizedBox(height: UiConstants.space1),
          Text(
            _formatTime(time),
            style: UiTypography.title1m
                .copyWith(fontWeight: FontWeight.w700)
                .textPrimary,
          ),
        ],
      ),
    );
  }
}
