//
//  Team.swift
//  SportsTime
//
//  Domain model for a sports team.
//
//  - Expected Behavior:
//    - fullName returns "city name" when city is non-empty
//    - fullName returns just name when city is empty
//    - Two teams are equal if and only if their ids match
//
//  - Invariants:
//    - fullName is never empty (name is required)
//    - Equality is based solely on id
//

import Foundation

struct Team: Identifiable, Codable, Hashable {
    let id: String  // Canonical ID: "team_mlb_bos"
    let name: String
    let abbreviation: String
    let sport: Sport
    let city: String
    let stadiumId: String  // FK: "stadium_mlb_fenway_park"
    let conferenceId: String?  // FK: "nba_eastern", "mlb_al"
    let divisionId: String?    // FK: "nba_southeast", "mlb_al_east"
    let logoURL: URL?
    let primaryColor: String?
    let secondaryColor: String?

    init(
        id: String,
        name: String,
        abbreviation: String,
        sport: Sport,
        city: String,
        stadiumId: String,
        conferenceId: String? = nil,
        divisionId: String? = nil,
        logoURL: URL? = nil,
        primaryColor: String? = nil,
        secondaryColor: String? = nil
    ) {
        self.id = id
        self.name = name
        self.abbreviation = abbreviation
        self.sport = sport
        self.city = city
        self.stadiumId = stadiumId
        self.conferenceId = conferenceId
        self.divisionId = divisionId
        self.logoURL = logoURL
        self.primaryColor = primaryColor
        self.secondaryColor = secondaryColor
    }

    var fullName: String {
        city.isEmpty ? name : "\(city) \(name)"
    }
}

extension Team: Equatable {
    static func == (lhs: Team, rhs: Team) -> Bool {
        lhs.id == rhs.id
    }
}
