//
//  AnySport.swift
//  SportsTime
//
//  Protocol unifying Sport enum and DynamicSport for interchangeable use.
//
//  - Expected Behavior:
//    - isInSeason(for:) default implementation handles wrap-around seasons
//    - For normal seasons (start <= end): month >= start AND month <= end
//    - For wrap-around seasons (start > end): month >= start OR month <= end
//
//  - Invariants:
//    - isInSeason returns true for exactly the months in the season range
//    - seasonMonths start and end are always 1-12
//

import SwiftUI

/// Protocol that unifies Sport enum and DynamicSport
/// Allows both to be used interchangeably in UI and planning engine.
protocol AnySport: Identifiable, Hashable, Sendable {
    /// Unique identifier string (e.g., "MLB", "xfl")
    var sportId: String { get }

    /// Display name for UI (e.g., "Major League Baseball", "XFL Football")
    var displayName: String { get }

    /// SF Symbol name for sport icon (e.g., "baseball.fill")
    var iconName: String { get }

    /// Theme color for this sport
    var color: Color { get }

    /// Season months (start and end, 1-12). End may be less than start for wrap-around seasons.
    var seasonMonths: (start: Int, end: Int) { get }
}

extension AnySport {
    /// Check if sport is in season for a given date
    func isInSeason(for date: Date) -> Bool {
        let calendar = Calendar.current
        let month = calendar.component(.month, from: date)

        let (start, end) = seasonMonths
        if start <= end {
            // Normal range (e.g., March to October)
            return month >= start && month <= end
        } else {
            // Season wraps around year boundary (e.g., October to June)
            return month >= start || month <= end
        }
    }
}
