import XCTest

final class CommitHistoryUITests: ForgejoReadOnlyUITestBase {

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

    // MARK: - Helpers

    @MainActor
    private func navigateToCommitHistory() {
        navigateToRepoDetail()
        let commitsButton = app.buttons["commits-button"]
        XCTAssertTrue(commitsButton.waitForExistence(timeout: 10), "Commits button not found")
        commitsButton.tap()
    }

    // MARK: - Commit History

    @MainActor
    func testCommitHistoryNavigation() throws {
        navigateToCommitHistory()

        // Commits list should appear with rows from seed data
        let commitList = app.collectionViews.firstMatch
        XCTAssertTrue(commitList.waitForExistence(timeout: 10), "Commit list not found")

        let firstCommitCell = commitList.cells.firstMatch
        XCTAssertTrue(firstCommitCell.waitForExistence(timeout: 10), "No commit rows found")

        // The test repo has multiple commits from setup (auto_init + file creates)
        XCTAssertTrue(commitList.cells.count >= 2, "Should have at least 2 commits from seed data")

        // Verify cells contain text (commit messages rendered)
        let cellTexts = firstCommitCell.staticTexts
        XCTAssertTrue(cellTexts.count >= 1, "Commit cells should contain text content")

        // Tap a commit to view its detail with diff
        firstCommitCell.tap()

        // The Changes section should appear with diff content
        let changesHeader = app.staticTexts["Changes"]
        XCTAssertTrue(changesHeader.waitForExistence(timeout: 10), "Changes section not found in commit detail")
    }
}
