import XCTest
@testable import Flights

/// Coverage for `DataIntegrityMonitor` — the shared sink that collects
/// bundled-JSON decode failures so `RootView` can show a banner instead
/// of leaving the user staring at "no data" with no explanation.
///
/// The monitor is `@MainActor` because it's read by SwiftUI views, so
/// every test hop onto the main actor before touching it. Each test also
/// calls `clear()` first because the singleton is process-wide and other
/// loaders may have reported into it during test bring-up.
@MainActor
final class DataIntegrityMonitorTests: XCTestCase {

    override func setUp() async throws {
        try await super.setUp()
        DataIntegrityMonitor.shared.clear()
    }

    override func tearDown() async throws {
        DataIntegrityMonitor.shared.clear()
        try await super.tearDown()
    }

    func test_reportingOneFailure_setsHasFailuresTrue() {
        let monitor = DataIntegrityMonitor.shared
        XCTAssertFalse(monitor.hasFailures, "monitor should start empty after clear()")

        let err = NSError(
            domain: "TestDomain",
            code: 1,
            userInfo: [NSLocalizedDescriptionKey: "bad json"]
        )
        monitor.report("bts_bundle.json", error: err)

        XCTAssertTrue(monitor.hasFailures)
        XCTAssertEqual(monitor.failures.count, 1)
        XCTAssertTrue(
            monitor.failures[0].contains("bts_bundle.json"),
            "failure entry should include the resource basename"
        )
        XCTAssertTrue(
            monitor.failures[0].contains("bad json"),
            "failure entry should include the localized description"
        )
    }

    func test_reportingTwoFailures_accumulates() {
        let monitor = DataIntegrityMonitor.shared

        monitor.report(
            "jumpseat_rules.json",
            error: NSError(domain: "T", code: 1, userInfo: [NSLocalizedDescriptionKey: "missing field"])
        )
        monitor.report(
            "crewbases.json",
            error: NSError(domain: "T", code: 2, userInfo: [NSLocalizedDescriptionKey: "trailing comma"])
        )

        XCTAssertEqual(monitor.failures.count, 2)
        XCTAssertTrue(monitor.hasFailures)
        XCTAssertTrue(monitor.failures[0].contains("jumpseat_rules.json"))
        XCTAssertTrue(monitor.failures[1].contains("crewbases.json"))
    }

    func test_clear_resetsHasFailures() {
        let monitor = DataIntegrityMonitor.shared

        monitor.report(
            "partner_matrix.json",
            error: NSError(domain: "T", code: 1, userInfo: [NSLocalizedDescriptionKey: "broken"])
        )
        XCTAssertTrue(monitor.hasFailures, "precondition: monitor has at least one failure")

        monitor.clear()

        XCTAssertFalse(monitor.hasFailures)
        XCTAssertEqual(monitor.failures.count, 0)
    }
}
