import 'package:equatable/equatable.dart';
import 'package:krow_domain/krow_domain.dart';

abstract class AvailabilityState extends Equatable {
  const AvailabilityState();
  @override
  List<Object?> get props => [];
}

class AvailabilityInitial extends AvailabilityState {}

class AvailabilityLoading extends AvailabilityState {}

class AvailabilityLoaded extends AvailabilityState {
  final List<DayAvailability> days;
  final DateTime currentWeekStart;
  final DateTime selectedDate;

  const AvailabilityLoaded({
    required this.days,
    required this.currentWeekStart,
    required this.selectedDate,
  });

  /// Helper to get the currently selected day's availability object
  DayAvailability get selectedDayAvailability {
    return days.firstWhere(
      (d) => isSameDay(d.date, selectedDate),
      orElse: () => DayAvailability(date: selectedDate), // Fallback
    );
  }

  AvailabilityLoaded copyWith({
    List<DayAvailability>? days,
    DateTime? currentWeekStart,
    DateTime? selectedDate,
  }) {
    return AvailabilityLoaded(
      days: days ?? this.days,
      currentWeekStart: currentWeekStart ?? this.currentWeekStart,
      selectedDate: selectedDate ?? this.selectedDate,
    );
  }

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

  @override
  List<Object?> get props => [days, currentWeekStart, selectedDate];
}

class AvailabilityError extends AvailabilityState {
  final String message;
  const AvailabilityError(this.message);
  @override
  List<Object?> get props => [message];
}
