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

class DateSelector extends StatelessWidget {
  const DateSelector({
    super.key,
    required this.selectedDate,
    required this.onSelect,
    this.shiftDates = const <String>[],
  });
  final DateTime selectedDate;
  final ValueChanged<DateTime> onSelect;
  final List<String> shiftDates;

  @override
  Widget build(BuildContext context) {
    final DateTime today = DateTime.now();
    final List<DateTime> dates = List.generate(7, (int index) {
      return today.add(Duration(days: index - 3));
    });

    return SizedBox(
      height: 80,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: dates.map((DateTime date) {
          final bool isSelected = _isSameDay(date, selectedDate);
          final bool isToday = _isSameDay(date, today);
          final bool hasShift = shiftDates.contains(_formatDateIso(date));

          return Expanded(
            child: GestureDetector(
              onTap: () => onSelect(date),
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 200),
                margin: const EdgeInsets.symmetric(
                  horizontal: UiConstants.space1,
                ),
                decoration: BoxDecoration(
                  color: isSelected ? UiColors.primary : UiColors.white,
                  borderRadius: UiConstants.radiusLg,
                  boxShadow: isSelected
                      ? <BoxShadow>[
                          BoxShadow(
                            color: UiColors.primary.withValues(alpha: 0.3),
                            blurRadius: 10,
                            offset: const Offset(0, 4),
                          ),
                        ]
                      : <BoxShadow>[],
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      DateFormat('d').format(date),
                      style: UiTypography.title1m.copyWith(
                        fontWeight: FontWeight.bold,
                        color:
                            isSelected ? UiColors.white : UiColors.foreground,
                      ),
                    ),
                    const SizedBox(height: 2),
                    Text(
                      DateFormat('E').format(date),
                      style: UiTypography.footnote2r.copyWith(
                        color: isSelected
                            ? UiColors.white.withValues(alpha: 0.8)
                            : UiColors.textInactive,
                      ),
                    ),
                    const SizedBox(height: UiConstants.space1),
                    if (hasShift)
                      Container(
                        width: 6,
                        height: 6,
                        decoration: BoxDecoration(
                          color: isSelected ? UiColors.white : UiColors.primary,
                          shape: BoxShape.circle,
                        ),
                      )
                    else if (isToday && !isSelected)
                      Container(
                        width: 6,
                        height: 6,
                        decoration: const BoxDecoration(
                          color: UiColors.border,
                          shape: BoxShape.circle,
                        ),
                      )
                    else
                      const SizedBox(height: 6),
                  ],
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  bool _isSameDay(DateTime a, DateTime b) {
    return a.year == b.year && a.month == b.month && a.day == b.day;
  }

  String _formatDateIso(DateTime date) {
    return DateFormat('yyyy-MM-dd').format(date);
  }
}
