import Foundation

struct FlightSchedule: Identifiable, Sendable {
    let id = UUID()
    let airline: Airline
    let flightNumber: String
    let aircraft: String
    let aircraftId: String
    let departureTime: String
    let arrivalTime: String
    let dateFrom: Date
    let dateTo: Date
    let daysOfWeek: Set<Int>
    let cabinClasses: CabinClass

    func operatesOn(date: Date) -> Bool {
        var utc = Calendar(identifier: .gregorian)
        utc.timeZone = TimeZone(identifier: "UTC")!

        let fromDay = utc.startOfDay(for: dateFrom)
        let toDay = utc.startOfDay(for: dateTo)

        // Normalize the input date to UTC noon to avoid timezone boundary issues
        let components = Calendar.current.dateComponents([.year, .month, .day], from: date)
        guard let normalizedDate = utc.date(from: components) else { return false }

        guard normalizedDate >= fromDay, normalizedDate <= toDay else { return false }

        // Use the local calendar for weekday since the user picked the date locally
        let weekday = Calendar.current.component(.weekday, from: date)
        return daysOfWeek.contains(weekday)
    }
}
