//
//  CanonicalSportTests.swift
//  SportsTimeTests
//
//  Tests for CanonicalSport SwiftData model and its conversion to DynamicSport domain model.
//

import XCTest
@testable import SportsTime

/// Tests for CanonicalSport model
/// Note: These tests verify the model's initialization and toDomain() conversion without
/// requiring a full SwiftData container, since the @Model macro generates the persistence layer.
final class CanonicalSportTests: XCTestCase {

    func test_CanonicalSport_ConvertsToDynamicSportDomainModel() {
        // Given: A CanonicalSport instance
        let canonical = CanonicalSport(
            id: "xfl",
            abbreviation: "XFL",
            displayName: "XFL Football",
            iconName: "football.fill",
            colorHex: "#E31837",
            seasonStartMonth: 2,
            seasonEndMonth: 5,
            isActive: true
        )

        // When: Converting to domain model
        let domain = canonical.toDomain()

        // Then: All properties are correctly mapped
        XCTAssertEqual(domain.id, "xfl")
        XCTAssertEqual(domain.abbreviation, "XFL")
        XCTAssertEqual(domain.displayName, "XFL Football")
        XCTAssertEqual(domain.iconName, "football.fill")
        XCTAssertEqual(domain.colorHex, "#E31837")
        XCTAssertEqual(domain.seasonStartMonth, 2)
        XCTAssertEqual(domain.seasonEndMonth, 5)
    }

    func test_CanonicalSport_InitializesWithDefaultValues() {
        // Given/When: Creating a CanonicalSport with only required parameters
        let sport = CanonicalSport(
            id: "test",
            abbreviation: "TST",
            displayName: "Test Sport",
            iconName: "star.fill",
            colorHex: "#000000",
            seasonStartMonth: 1,
            seasonEndMonth: 12
        )

        // Then: Default values are set correctly
        XCTAssertTrue(sport.isActive)
        XCTAssertEqual(sport.schemaVersion, SchemaVersion.current)
        XCTAssertEqual(sport.source, .cloudKit)
    }

    func test_CanonicalSport_SourcePropertyWorksCorrectly() {
        // Given: A CanonicalSport
        let sport = CanonicalSport(
            id: "test",
            abbreviation: "TST",
            displayName: "Test Sport",
            iconName: "star.fill",
            colorHex: "#000000",
            seasonStartMonth: 1,
            seasonEndMonth: 12,
            source: .bundled
        )

        // Then: Source is correctly stored and retrieved
        XCTAssertEqual(sport.source, .bundled)

        // When: Changing the source
        sport.source = .userCorrection

        // Then: Source is updated
        XCTAssertEqual(sport.source, .userCorrection)
        XCTAssertEqual(sport.sourceRaw, "userCorrection")
    }

    func test_CanonicalSport_HasUniqueIdAttribute() {
        // Given: Two CanonicalSport instances with the same id
        let sport1 = CanonicalSport(
            id: "xfl",
            abbreviation: "XFL",
            displayName: "XFL Football",
            iconName: "football.fill",
            colorHex: "#E31837",
            seasonStartMonth: 2,
            seasonEndMonth: 5
        )

        let sport2 = CanonicalSport(
            id: "xfl",
            abbreviation: "XFL",
            displayName: "XFL Football Updated",
            iconName: "football.fill",
            colorHex: "#E31837",
            seasonStartMonth: 2,
            seasonEndMonth: 5
        )

        // Then: Both instances have the same id (SwiftData's @Attribute(.unique) handles uniqueness at persistence level)
        XCTAssertEqual(sport1.id, sport2.id)
    }
}
