import XCTest

/// Critical path tests for core navigation.
/// Validates tab bar presence, navigation, settings access, and add buttons.
final class NavigationCriticalPathTests: AuthenticatedUITestCase {

    override var needsAPISession: Bool { true }

    override func setUpWithError() throws {
        try super.setUpWithError()
        // Precondition: residence must exist for task add button to appear
        ensureResidenceExists()
    }

    // MARK: - Tab Navigation

    func testAllTabsExist() {
        let tabBar = app.tabBars.firstMatch
        XCTAssertTrue(tabBar.exists, "Tab bar should exist after login")

        let residences = tabBar.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Residences'")).firstMatch
        let tasks = tabBar.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Tasks'")).firstMatch
        let contractors = tabBar.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Contractors'")).firstMatch
        let documents = tabBar.buttons.containing(NSPredicate(format: "label CONTAINS[c] 'Doc'")).firstMatch

        XCTAssertTrue(residences.exists, "Residences tab should exist")
        XCTAssertTrue(tasks.exists, "Tasks tab should exist")
        XCTAssertTrue(contractors.exists, "Contractors tab should exist")
        XCTAssertTrue(documents.exists, "Documents tab should exist")
    }

    func testNavigateToTasksTab() {
        navigateToTasks()
        // Verify by checking for Tasks screen content, not isSelected (unreliable with sidebarAdaptable)
        let addButton = app.buttons[AccessibilityIdentifiers.Task.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: navigationTimeout), "Tasks screen should show add button")
    }

    func testNavigateToContractorsTab() {
        navigateToContractors()
        let addButton = app.buttons[AccessibilityIdentifiers.Contractor.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: navigationTimeout), "Contractors screen should show add button")
    }

    func testNavigateToDocumentsTab() {
        navigateToDocuments()
        let addButton = app.buttons[AccessibilityIdentifiers.Document.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: navigationTimeout), "Documents screen should show add button")
    }

    func testNavigateBackToResidencesTab() {
        navigateToDocuments()
        navigateToResidences()
        let addButton = app.buttons[AccessibilityIdentifiers.Residence.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: navigationTimeout), "Residences screen should show add button")
    }

    // MARK: - Settings Access

    func testSettingsButtonExists() {
        navigateToResidences()
        let settingsButton = app.buttons[AccessibilityIdentifiers.Navigation.settingsButton]
        XCTAssertTrue(settingsButton.waitForExistence(timeout: defaultTimeout), "Settings button should exist on Residences screen")
    }

    // MARK: - Add Buttons

    func testResidenceAddButtonExists() {
        navigateToResidences()
        let addButton = app.buttons[AccessibilityIdentifiers.Residence.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: defaultTimeout), "Residence add button should exist")
    }

    func testTaskAddButtonExists() {
        navigateToTasks()
        let addButton = app.buttons[AccessibilityIdentifiers.Task.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: defaultTimeout), "Task add button should exist")
    }

    func testContractorAddButtonExists() {
        navigateToContractors()
        let addButton = app.buttons[AccessibilityIdentifiers.Contractor.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: defaultTimeout), "Contractor add button should exist")
    }

    func testDocumentAddButtonExists() {
        navigateToDocuments()
        let addButton = app.buttons[AccessibilityIdentifiers.Document.addButton].firstMatch
        XCTAssertTrue(addButton.waitForExistence(timeout: defaultTimeout), "Document add button should exist")
    }
}
