//
//  Stadium.swift
//  SportsTime
//

import Foundation
import CoreLocation

struct Stadium: Identifiable, Codable, Hashable {
    let id: UUID
    let name: String
    let city: String
    let state: String
    let latitude: Double
    let longitude: Double
    let capacity: Int
    let sport: Sport
    let yearOpened: Int?
    let imageURL: URL?

    init(
        id: UUID,
        name: String,
        city: String,
        state: String,
        latitude: Double,
        longitude: Double,
        capacity: Int,
        sport: Sport,
        yearOpened: Int? = nil,
        imageURL: URL? = nil
    ) {
        self.id = id
        self.name = name
        self.city = city
        self.state = state
        self.latitude = latitude
        self.longitude = longitude
        self.capacity = capacity
        self.sport = sport
        self.yearOpened = yearOpened
        self.imageURL = imageURL
    }

    var location: CLLocation {
        CLLocation(latitude: latitude, longitude: longitude)
    }

    var coordinate: CLLocationCoordinate2D {
        CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }

    var fullAddress: String {
        "\(city), \(state)"
    }

    var region: Region {
        Region.classify(longitude: longitude)
    }

    func distance(to other: Stadium) -> CLLocationDistance {
        location.distance(from: other.location)
    }

    func distance(from coordinate: CLLocationCoordinate2D) -> CLLocationDistance {
        let otherLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
        return location.distance(from: otherLocation)
    }
}

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