import XCTest

final class ActionsUITests: ForgejoReadOnlyUITestBase {

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

    /// The Actions tab is only present for repos with `has_actions: true`.
    /// The seed leaves `test-repo` at the Forgejo-15 default (actions enabled),
    /// so the tab is expected to render and produce the workflow status filter.
    @MainActor
    func testActionsTabRendersWhenAvailable() throws {
        navigateToRepoDetail("test-repo")

        let tabPicker = app.segmentedControls["repo-detail-tab-picker"]
        XCTAssertTrue(tabPicker.waitForExistence(timeout: 10), "Tab picker should appear")

        let actionsTab = tabPicker.buttons["Actions"]
        XCTAssertTrue(
            actionsTab.waitForExistence(timeout: 3),
            "Actions tab should be exposed for test-repo (Forgejo 15 default has_actions: true)",
        )

        actionsTab.tap()

        // Status filter (All/Running/Success/Failed) should render.
        // The picker itself is an HStack, which doesn't surface as an XCUI element,
        // so probe a uniquely-named child button instead of the wrapper identifier.
        let runningButton = app.buttons["Running"]
        XCTAssertTrue(runningButton.waitForExistence(timeout: 5), "Workflow run status picker should render")
        XCTAssertTrue(app.buttons["Success"].exists, "Success filter button should render")
        XCTAssertTrue(app.buttons["Failed"].exists, "Failed filter button should render")
    }

    @MainActor
    func testActionsTabAbsentForRepoWithoutActions() throws {
        navigateToRepoDetail("test-repo-2")

        let tabPicker = app.segmentedControls["repo-detail-tab-picker"]
        XCTAssertTrue(tabPicker.waitForExistence(timeout: 10))

        // If the repo has has_actions: false (the default), the Actions tab must not appear.
        // This guards the availableTabs filter from showing actions on every repo.
        let actionsTab = tabPicker.buttons["Actions"]
        if actionsTab.waitForExistence(timeout: 2) {
            // If Actions IS present, the seed has been updated to enable actions on this repo.
            // The remaining assertions don't apply; nothing to verify here for the negative case.
            return
        }
        XCTAssertFalse(actionsTab.exists)
    }
}
