//
//  Division.swift
//  SportsTime
//
//  Domain model for league structure: divisions and conferences.
//
//  - Expected Behavior:
//    - LeagueStructure.divisions(for:) returns divisions for MLB, NBA, NHL; empty for others
//    - LeagueStructure.conferences(for:) returns conferences filtered by sport
//    - LeagueStructure.division(byId:) finds division across all sports
//    - LeagueStructure.conference(byId:) finds conference across all sports
//    - stadiumCount returns 30 for MLB, 30 for NBA, 32 for NHL, 0 for others
//
//  - Invariants:
//    - Division.teamCount == teamCanonicalIds.count
//    - MLB has 6 divisions (3 per league), NBA has 6, NHL has 4
//    - Each conference contains valid division IDs
//

import Foundation

// MARK: - Division

struct Division: Identifiable, Codable, Hashable {
    let id: String              // e.g., "mlb_nl_west"
    let name: String            // e.g., "NL West"
    let conference: String      // e.g., "National League"
    let conferenceId: String    // e.g., "mlb_nl"
    let sport: Sport
    var teamCanonicalIds: [String]  // Canonical team IDs in this division

    var teamCount: Int { teamCanonicalIds.count }
}

// MARK: - Conference

struct Conference: Identifiable, Codable, Hashable {
    let id: String              // e.g., "mlb_nl"
    let name: String            // e.g., "National League"
    let abbreviation: String?   // e.g., "NL"
    let sport: Sport
    let divisionIds: [String]   // Division IDs in this conference
}

// MARK: - League Structure Provider

/// Provides access to league structure data (divisions, conferences).
/// Reads from SwiftData LeagueStructureModel and CanonicalTeam.
enum LeagueStructure {

    // MARK: - Static Division Definitions

    /// All MLB divisions
    static let mlbDivisions: [Division] = [
        Division(id: "mlb_al_east", name: "AL East", conference: "American League", conferenceId: "mlb_al", sport: .mlb, teamCanonicalIds: []),
        Division(id: "mlb_al_central", name: "AL Central", conference: "American League", conferenceId: "mlb_al", sport: .mlb, teamCanonicalIds: []),
        Division(id: "mlb_al_west", name: "AL West", conference: "American League", conferenceId: "mlb_al", sport: .mlb, teamCanonicalIds: []),
        Division(id: "mlb_nl_east", name: "NL East", conference: "National League", conferenceId: "mlb_nl", sport: .mlb, teamCanonicalIds: []),
        Division(id: "mlb_nl_central", name: "NL Central", conference: "National League", conferenceId: "mlb_nl", sport: .mlb, teamCanonicalIds: []),
        Division(id: "mlb_nl_west", name: "NL West", conference: "National League", conferenceId: "mlb_nl", sport: .mlb, teamCanonicalIds: [])
    ]

    /// All NBA divisions
    static let nbaDivisions: [Division] = [
        Division(id: "nba_atlantic", name: "Atlantic", conference: "Eastern Conference", conferenceId: "nba_eastern", sport: .nba, teamCanonicalIds: []),
        Division(id: "nba_central", name: "Central", conference: "Eastern Conference", conferenceId: "nba_eastern", sport: .nba, teamCanonicalIds: []),
        Division(id: "nba_southeast", name: "Southeast", conference: "Eastern Conference", conferenceId: "nba_eastern", sport: .nba, teamCanonicalIds: []),
        Division(id: "nba_northwest", name: "Northwest", conference: "Western Conference", conferenceId: "nba_western", sport: .nba, teamCanonicalIds: []),
        Division(id: "nba_pacific", name: "Pacific", conference: "Western Conference", conferenceId: "nba_western", sport: .nba, teamCanonicalIds: []),
        Division(id: "nba_southwest", name: "Southwest", conference: "Western Conference", conferenceId: "nba_western", sport: .nba, teamCanonicalIds: [])
    ]

    /// All NHL divisions
    static let nhlDivisions: [Division] = [
        Division(id: "nhl_atlantic", name: "Atlantic", conference: "Eastern Conference", conferenceId: "nhl_eastern", sport: .nhl, teamCanonicalIds: []),
        Division(id: "nhl_metropolitan", name: "Metropolitan", conference: "Eastern Conference", conferenceId: "nhl_eastern", sport: .nhl, teamCanonicalIds: []),
        Division(id: "nhl_central", name: "Central", conference: "Western Conference", conferenceId: "nhl_western", sport: .nhl, teamCanonicalIds: []),
        Division(id: "nhl_pacific", name: "Pacific", conference: "Western Conference", conferenceId: "nhl_western", sport: .nhl, teamCanonicalIds: [])
    ]

    /// All conferences
    static let conferences: [Conference] = [
        // MLB
        Conference(id: "mlb_al", name: "American League", abbreviation: "AL", sport: .mlb, divisionIds: ["mlb_al_east", "mlb_al_central", "mlb_al_west"]),
        Conference(id: "mlb_nl", name: "National League", abbreviation: "NL", sport: .mlb, divisionIds: ["mlb_nl_east", "mlb_nl_central", "mlb_nl_west"]),
        // NBA
        Conference(id: "nba_eastern", name: "Eastern Conference", abbreviation: "East", sport: .nba, divisionIds: ["nba_atlantic", "nba_central", "nba_southeast"]),
        Conference(id: "nba_western", name: "Western Conference", abbreviation: "West", sport: .nba, divisionIds: ["nba_northwest", "nba_pacific", "nba_southwest"]),
        // NHL
        Conference(id: "nhl_eastern", name: "Eastern Conference", abbreviation: "East", sport: .nhl, divisionIds: ["nhl_atlantic", "nhl_metropolitan"]),
        Conference(id: "nhl_western", name: "Western Conference", abbreviation: "West", sport: .nhl, divisionIds: ["nhl_central", "nhl_pacific"])
    ]

    // MARK: - Lookup Methods

    /// Get all divisions for a sport
    static func divisions(for sport: Sport) -> [Division] {
        switch sport {
        case .mlb: return mlbDivisions
        case .nba: return nbaDivisions
        case .nhl: return nhlDivisions
        default: return []
        }
    }

    /// Get all conferences for a sport
    static func conferences(for sport: Sport) -> [Conference] {
        conferences.filter { $0.sport == sport }
    }

    /// Find division by ID
    static func division(byId id: String) -> Division? {
        let allDivisions = mlbDivisions + nbaDivisions + nhlDivisions
        return allDivisions.first { $0.id == id }
    }

    /// Find conference by ID
    static func conference(byId id: String) -> Conference? {
        conferences.first { $0.id == id }
    }

    /// Get total stadium count for a sport
    static func stadiumCount(for sport: Sport) -> Int {
        switch sport {
        case .mlb: return 30
        case .nba: return 30
        case .nhl: return 32
        default: return 0
        }
    }
}
