import XCTest

final class IssueMutatingUITests: ForgejoUITestBase {

    // MARK: - Issue Tab (list, filter, detail, label, create, edit, close/reopen)

    @MainActor
    func testIssueTab() throws {
        loginAndWaitForHome()
        navigateToIssueTab()

        // Open issues visible
        XCTAssertTrue(app.staticTexts["Test issue 1 from integration tests"].waitForExistence(timeout: 10))
        let issueList = app.collectionViews.firstMatch
        XCTAssertTrue(issueList.cells.count >= 2, "Should have at least 2 open issues")

        // Filter by Closed
        let closedButton = app.buttons["Closed"]
        XCTAssertTrue(closedButton.waitForExistence(timeout: 10))
        closedButton.tap()
        XCTAssertTrue(app.staticTexts["Test issue 3 from integration tests"].waitForExistence(timeout: 10))

        // Back to Open
        let openButton = app.buttons["Open"]
        XCTAssertTrue(openButton.waitForExistence(timeout: 5))
        openButton.tap()
        XCTAssertTrue(app.staticTexts["Test issue 1 from integration tests"].waitForExistence(timeout: 10))

        // Issue detail, title, comments, label, milestone, assignee
        let issueCell = app.staticTexts["Test issue 1 from integration tests"].firstMatch
        issueCell.tap()

        let titleText = app.staticTexts["issue-detail-title"]
        XCTAssertTrue(titleText.waitForExistence(timeout: 10))
        XCTAssertTrue(app.staticTexts["bug"].waitForExistence(timeout: 10))

        // Milestone and assignee (may need scroll to see assignee section)
        XCTAssertTrue(app.staticTexts["v1.0"].waitForExistence(timeout: 5), "Milestone should be visible")
        app.swipeUp()
        app.swipeUp()
        XCTAssertTrue(app.staticTexts["@testadmin"].waitForExistence(timeout: 5), "Assignee should be visible")
        XCTAssertTrue(app.staticTexts["testbot"].waitForExistence(timeout: 5))
        app.swipeDown()
        app.swipeDown()

        // Back to issue list
        app.navigationBars.buttons.firstMatch.tap()
        XCTAssertTrue(app.staticTexts["Test issue 1 from integration tests"].waitForExistence(timeout: 10))

        // Note: Create button is inside a ZStack within a paging TabView,
        // which clips accessibility elements. Create is tested via overview.

        // Edit issue #2 title
        let issue2Cell = app.staticTexts["Test issue 2 from integration tests"].firstMatch
        XCTAssertTrue(issue2Cell.waitForExistence(timeout: 10))
        issue2Cell.tap()

        XCTAssertTrue(app.staticTexts["issue-detail-title"].waitForExistence(timeout: 10))

        expandActionMenu()
        let issueEditButton = app.buttons["issue-edit-button"]
        XCTAssertTrue(issueEditButton.waitForExistence(timeout: 5))
        issueEditButton.tap()

        let editTitleField = app.textFields["issue-edit-title-field"]
        XCTAssertTrue(editTitleField.waitForExistence(timeout: 5))
        editTitleField.tap(withNumberOfTaps: 3, numberOfTouches: 1)
        editTitleField.typeText("Edited issue title")

        // Edit the description. Tapping the description row must open the editor
        // sheet, the regression from issue #80 was that the selectable markdown
        // preview swallowed the tap so the sheet never appeared.
        let descriptionButton = app.buttons["description-edit-button"]
        XCTAssertTrue(descriptionButton.waitForExistence(timeout: 5))
        descriptionButton.tap()

        let descriptionEditor = app.textViews["markdown-text-editor"]
        XCTAssertTrue(
            descriptionEditor.waitForExistence(timeout: 5),
            "Tapping the description should open the editor sheet",
        )
        descriptionEditor.tap()
        descriptionEditor.typeText("UITESTDESC ")
        app.buttons["Done"].tap()

        let saveButton = app.buttons["issue-edit-save"]
        XCTAssertTrue(saveButton.waitForExistence(timeout: 5))
        saveButton.tap()
        XCTAssertTrue(app.staticTexts["Edited issue title"].waitForExistence(timeout: 10))

        // The edited description must be persisted and rendered in the detail view.
        let editedDescription = app.staticTexts.element(
            matching: NSPredicate(format: "label CONTAINS %@", "UITESTDESC"),
        )
        XCTAssertTrue(
            editedDescription.waitForExistence(timeout: 10),
            "Edited description should appear in the issue detail",
        )

        // Close and reopen
        expandActionMenu()
        let toggleButton = app.buttons["issue-toggle-state"]
        XCTAssertTrue(toggleButton.waitForExistence(timeout: 10))
        toggleButton.tap()
        XCTAssertTrue(app.staticTexts["Closed"].waitForExistence(timeout: 10))

        expandActionMenu()
        let reopenButton = app.buttons["issue-toggle-state"]
        XCTAssertTrue(reopenButton.waitForExistence(timeout: 5))
        reopenButton.tap()
        XCTAssertTrue(app.staticTexts["Open"].waitForExistence(timeout: 10))
    }
}
