//
//  DynamicSport.swift
//  SportsTime
//
//  Domain model for CloudKit-defined sports.
//
//  - Expected Behavior:
//    - sportId returns id (from AnySport protocol)
//    - color converts colorHex to SwiftUI Color
//    - seasonMonths returns (seasonStartMonth, seasonEndMonth)
//    - isInSeason uses default AnySport implementation
//
//  - Invariants:
//    - sportId == id (they are aliases)
//    - seasonStartMonth and seasonEndMonth are 1-12
//

import SwiftUI

/// A sport defined via CloudKit, as opposed to the hardcoded Sport enum.
/// Conforms to AnySport for interchangeable use with Sport enum.
nonisolated struct DynamicSport: Identifiable, Hashable, Codable, Sendable {
    let id: String
    let abbreviation: String
    let displayName: String
    let iconName: String
    let colorHex: String
    let seasonStartMonth: Int
    let seasonEndMonth: Int

    init(
        id: String,
        abbreviation: String,
        displayName: String,
        iconName: String,
        colorHex: String,
        seasonStartMonth: Int,
        seasonEndMonth: Int
    ) {
        self.id = id
        self.abbreviation = abbreviation
        self.displayName = displayName
        self.iconName = iconName
        self.colorHex = colorHex
        self.seasonStartMonth = seasonStartMonth
        self.seasonEndMonth = seasonEndMonth
    }
}

// MARK: - AnySport Conformance

extension DynamicSport: AnySport {
    var sportId: String { id }

    var color: Color { Color(hex: colorHex) }

    var seasonMonths: (start: Int, end: Int) {
        (seasonStartMonth, seasonEndMonth)
    }
}
