//
//  Sport.swift
//  SportsTime
//

import Foundation
import SwiftUI

enum Sport: String, Codable, CaseIterable, Identifiable {
    case mlb = "MLB"
    case nba = "NBA"
    case nhl = "NHL"
    case nfl = "NFL"
    case mls = "MLS"
    case wnba = "WNBA"
    case nwsl = "NWSL"

    var id: String { rawValue }

    var displayName: String {
        switch self {
        case .mlb: return "Major League Baseball"
        case .nba: return "National Basketball Association"
        case .nhl: return "National Hockey League"
        case .nfl: return "National Football League"
        case .mls: return "Major League Soccer"
        case .wnba: return "Women's National Basketball Association"
        case .nwsl: return "National Women's Soccer League"
        }
    }

    var iconName: String {
        switch self {
        case .mlb: return "baseball.fill"
        case .nba: return "basketball.fill"
        case .nhl: return "hockey.puck.fill"
        case .nfl: return "football.fill"
        case .mls: return "soccerball"
        case .wnba: return "basketball.fill"
        case .nwsl: return "soccerball"
        }
    }

    var color: Color {
        switch self {
        case .mlb: return .red
        case .nba: return .orange
        case .nhl: return .blue
        case .nfl: return .brown
        case .mls: return .green
        case .wnba: return .purple
        case .nwsl: return .teal
        }
    }

    /// Season start and end months (1-12). End may be less than start for seasons that wrap around the year.
    var seasonMonths: (start: Int, end: Int) {
        switch self {
        case .mlb: return (3, 10)   // March - October
        case .nba: return (10, 6)   // October - June (wraps)
        case .nhl: return (10, 6)   // October - June (wraps)
        case .nfl: return (9, 2)    // September - February (wraps)
        case .mls: return (2, 12)   // February - December
        case .wnba: return (5, 10)  // May - October
        case .nwsl: return (3, 11)  // March - November
        }
    }

    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
        }
    }

    /// Currently supported sports
    static var supported: [Sport] {
        [.mlb, .nba, .nfl, .nhl, .mls, .wnba, .nwsl]
    }
}

// MARK: - Array Chunking

extension Array {
    /// Splits array into chunks of specified size
    func chunked(into size: Int) -> [[Element]] {
        stride(from: 0, to: count, by: size).map {
            Array(self[$0..<Swift.min($0 + size, count)])
        }
    }
}
