//
//  TripStop.swift
//  SportsTime
//

import Foundation
import CoreLocation

struct TripStop: Identifiable, Codable, Hashable {
    let id: UUID
    let stopNumber: Int
    let city: String
    let state: String
    let coordinate: CLLocationCoordinate2D?
    let arrivalDate: Date
    let departureDate: Date
    let games: [String]
    let stadium: String?
    let lodging: LodgingSuggestion?
    let activities: [ActivitySuggestion]
    let isRestDay: Bool
    let notes: String?

    init(
        id: UUID = UUID(),
        stopNumber: Int,
        city: String,
        state: String,
        coordinate: CLLocationCoordinate2D? = nil,
        arrivalDate: Date,
        departureDate: Date,
        games: [String] = [],
        stadium: String? = nil,
        lodging: LodgingSuggestion? = nil,
        activities: [ActivitySuggestion] = [],
        isRestDay: Bool = false,
        notes: String? = nil
    ) {
        self.id = id
        self.stopNumber = stopNumber
        self.city = city
        self.state = state
        self.coordinate = coordinate
        self.arrivalDate = arrivalDate
        self.departureDate = departureDate
        self.games = games
        self.stadium = stadium
        self.lodging = lodging
        self.activities = activities
        self.isRestDay = isRestDay
        self.notes = notes
    }

    var stayDuration: Int {
        let days = Calendar.current.dateComponents([.day], from: arrivalDate, to: departureDate).day ?? 1
        return max(1, days)
    }

    var locationDescription: String { "\(city), \(state)" }
    var hasGames: Bool { !games.isEmpty }

    var formattedDateRange: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM d"
        let start = formatter.string(from: arrivalDate)
        let end = formatter.string(from: departureDate)
        return stayDuration > 1 ? "\(start) - \(end)" : start
    }
}

// MARK: - Lodging Suggestion

struct LodgingSuggestion: Identifiable, Codable, Hashable {
    let id: UUID
    let name: String
    let type: LodgingType
    let address: String?
    let coordinate: CLLocationCoordinate2D?
    let priceRange: PriceRange?
    let distanceToVenue: Double?
    let rating: Double?

    init(
        id: UUID,
        name: String,
        type: LodgingType,
        address: String? = nil,
        coordinate: CLLocationCoordinate2D? = nil,
        priceRange: PriceRange? = nil,
        distanceToVenue: Double? = nil,
        rating: Double? = nil
    ) {
        self.id = id
        self.name = name
        self.type = type
        self.address = address
        self.coordinate = coordinate
        self.priceRange = priceRange
        self.distanceToVenue = distanceToVenue
        self.rating = rating
    }
}

enum PriceRange: String, Codable, CaseIterable {
    case budget = "$"
    case moderate = "$$"
    case upscale = "$$$"
    case luxury = "$$$$"
}

// MARK: - Activity Suggestion

struct ActivitySuggestion: Identifiable, Codable, Hashable {
    let id: UUID
    let name: String
    let description: String?
    let category: ActivityCategory
    let estimatedDuration: TimeInterval
    let location: LocationInput?

    init(
        id: UUID = UUID(),
        name: String,
        description: String? = nil,
        category: ActivityCategory = .attraction,
        estimatedDuration: TimeInterval = 7200,
        location: LocationInput? = nil
    ) {
        self.id = id
        self.name = name
        self.description = description
        self.category = category
        self.estimatedDuration = estimatedDuration
        self.location = location
    }
}

enum ActivityCategory: String, Codable, CaseIterable {
    case attraction
    case food
    case entertainment
    case outdoors
    case shopping
    case culture

    var displayName: String {
        switch self {
        case .attraction: return "Attraction"
        case .food: return "Food & Drink"
        case .entertainment: return "Entertainment"
        case .outdoors: return "Outdoors"
        case .shopping: return "Shopping"
        case .culture: return "Culture"
        }
    }

    var iconName: String {
        switch self {
        case .attraction: return "star.fill"
        case .food: return "fork.knife"
        case .entertainment: return "theatermasks.fill"
        case .outdoors: return "leaf.fill"
        case .shopping: return "bag.fill"
        case .culture: return "building.columns.fill"
        }
    }
}
