//
//  SportsTimeTests.swift
//  SportsTimeTests
//
//  Created by Trey Tartt on 1/6/26.
//

import Testing
@testable import SportsTime
import Foundation

// MARK: - DayCard Tests (Removed)
// DayCard and DayConflictInfo types were removed during refactor.
// Tests for TripDetailView conflict detection are in TripDetailViewTests.swift if needed.

// MARK: - Duplicate Game ID Regression Tests

/// Tests for handling duplicate game IDs without crashing (regression test for fatal error)
struct DuplicateGameIdTests {

    private func makeStadium(sport: Sport = .mlb) -> Stadium {
        Stadium(
            id: UUID(),
            name: "Test Stadium",
            city: "Test City",
            state: "TS",
            latitude: 40.0,
            longitude: -100.0,
            capacity: 40000,
            sport: sport
        )
    }

    private func makeTeam(sport: Sport = .mlb, stadiumId: UUID) -> Team {
        Team(
            id: UUID(),
            name: "Test Team",
            abbreviation: "TST",
            sport: sport,
            city: "Test City",
            stadiumId: stadiumId
        )
    }

    private func makeGame(id: UUID, homeTeamId: UUID, awayTeamId: UUID, stadiumId: UUID, dateTime: Date) -> Game {
        Game(
            id: id,
            homeTeamId: homeTeamId,
            awayTeamId: awayTeamId,
            stadiumId: stadiumId,
            dateTime: dateTime,
            sport: .mlb,
            season: "2026"
        )
    }

    // Note: GameCandidate test removed - type no longer exists after planning engine refactor

    @Test("Duplicate games are deduplicated at load time")
    func gamesArray_DeduplicatesById() {
        // Simulate the deduplication logic used in StubDataProvider
        let gameId = UUID()
        let dateTime = Date()

        let game1 = makeGame(id: gameId, homeTeamId: UUID(), awayTeamId: UUID(), stadiumId: UUID(), dateTime: dateTime)
        let game2 = makeGame(id: gameId, homeTeamId: UUID(), awayTeamId: UUID(), stadiumId: UUID(), dateTime: dateTime.addingTimeInterval(3600))

        let games = [game1, game2]

        // Deduplication logic from StubDataProvider
        var seenIds = Set<UUID>()
        let uniqueGames = games.filter { game in
            if seenIds.contains(game.id) {
                return false
            }
            seenIds.insert(game.id)
            return true
        }

        #expect(uniqueGames.count == 1)
        #expect(uniqueGames.first?.dateTime == game1.dateTime, "First occurrence should be kept")
    }
}
