import XCTest

final class IssueUITests: ForgejoReadOnlyUITestBase {

    override func tearDown() {
        navigateBackToHome()
        super.tearDown()
    }

    // MARK: - Issues Overview (all issues, filter, search)

    @MainActor
    func testIssuesOverview() throws {
        app.tabBars.buttons["Issues"].tap()

        // Wait for initial load (default scope: Open + All involvement)
        XCTAssertTrue(app.cells.firstMatch.waitForExistence(timeout: 15), "Issues should load")

        // Search narrows results to a specific issue
        let searchField = app.searchFields.firstMatch
        XCTAssertTrue(searchField.waitForExistence(timeout: 5))
        searchField.tap()
        searchField.typeText("Pagination test issue 25")

        XCTAssertTrue(app.staticTexts["Pagination test issue 25"].waitForExistence(timeout: 15))
    }

    // MARK: - Issues Overview Involvement Filter

    @MainActor
    func testIssuesOverviewInvolvementFilter() throws {
        app.tabBars.buttons["Issues"].tap()

        // Reset filters to defaults (previous test may have changed them)
        resetOverviewFilters()

        // Wait for list to load
        XCTAssertTrue(app.cells.firstMatch.waitForExistence(timeout: 10), "Issues should load")

        // In the default state (Open + All involvement) the summary is hidden.
        let filterSummary = app.staticTexts["filter-summary"]
        XCTAssertFalse(filterSummary.exists, "Filter summary should be hidden in the default state")

        let filterMenuButton = app.buttons["filter-menu-button"]

        // Selecting a non-default scope reveals the summary.
        filterMenuButton.tap()
        app.buttons["Assigned to you"].tap()
        sleep(2)
        XCTAssertTrue(filterSummary.waitForExistence(timeout: 5))
        XCTAssertTrue(filterSummary.label.contains("Assigned to you"))

        // Tap "Mentioned" via filter menu
        filterMenuButton.tap()
        app.buttons["Mentioned"].tap()
        sleep(2)
        XCTAssertTrue(filterSummary.label.contains("Mentioned"))

        // Tap "Created by you" via filter menu
        filterMenuButton.tap()
        app.buttons["Created by you"].tap()
        sleep(2)
        XCTAssertTrue(filterSummary.label.contains("Created by you"))

        // "Review requested" should NOT be present for issues
        filterMenuButton.tap()
        XCTAssertFalse(app.buttons["Review requested"].exists, "Review filter should not appear for issues")
        // Dismiss the menu by tapping elsewhere
        app.tap()
    }

    // MARK: - All State + All Scope loads items

    @MainActor
    func testAllStateAllScopeLoadsIssues() throws {
        app.tabBars.buttons["Issues"].tap()

        let filterSummary = app.staticTexts["filter-summary"]
        let filterMenuButton = app.buttons["filter-menu-button"]
        XCTAssertTrue(filterMenuButton.waitForExistence(timeout: 10))

        // Reset to known state: Open + Created by you
        filterMenuButton.tap()
        app.buttons["Open"].tap()
        sleep(1)
        filterMenuButton.tap()
        app.buttons["Created by you"].tap()

        XCTAssertTrue(app.cells.firstMatch.waitForExistence(timeout: 10),
                       "Issues should load with default Open + Created filters")
        XCTAssertEqual(filterSummary.label, "Open · Created by you")

        // Step 1: Change state to All (first "All" in menu is from State section)
        filterMenuButton.tap()
        app.buttons["All"].firstMatch.tap()

        XCTAssertTrue(app.cells.firstMatch.waitForExistence(timeout: 10),
                       "Issues should load after state changed to All")
        XCTAssertEqual(filterSummary.label, "All · Created by you")

        // Step 2: Change scope to All (second "All" in menu is from Scope section)
        filterMenuButton.tap()
        let allButtons = app.buttons.matching(identifier: "All")
        XCTAssertGreaterThanOrEqual(allButtons.count, 2,
                                     "Menu should have at least 2 'All' buttons (State + Scope)")
        allButtons.element(boundBy: 1).tap()

        XCTAssertTrue(filterSummary.waitForExistence(timeout: 5))
        XCTAssertEqual(filterSummary.label, "All · All involvement",
                        "Filter summary should show All state and All scope")

        // The key assertion: items must actually load
        XCTAssertTrue(app.cells.firstMatch.waitForExistence(timeout: 10),
                       "Issues MUST load with All state + All scope (involved)")
    }
}
