import XCTest

final class LoginUITests: ForgejoUITestBase {

    // MARK: - Login Bad Credentials

    @MainActor
    func testLoginBadCredentials() throws {
        // Skip auto-login entirely so the instance list appears immediately,
        // regardless of stored tokens/sessions from previous test classes.
        app.launchArguments = [
            "-dev_serverURL", serverURL!,
            "-dev_username", "testadmin",
            "-dev_password", "wrongpassword",
            "-dev_skipAutoLogin", "true",
        ]
        app.launch()

        let addButton = app.buttons["instance-add-button"]
        XCTAssertTrue(addButton.waitForExistence(timeout: 15), "Instance list did not appear")
        addButton.tap()

        // Scroll form sheet to reveal login button (may be below the fold)
        app.swipeUp()

        let loginButton = app.buttons["login-button"]
        XCTAssertTrue(loginButton.waitForExistence(timeout: 5))
        loginButton.tap()

        let alert = app.alerts["Login Failed"]
        XCTAssertTrue(alert.waitForExistence(timeout: 10))
    }

    // MARK: - Token Login Success

    @MainActor
    func testLoginWithToken() throws {
        guard let token = Self.resolveTestToken(), !token.isEmpty else {
            throw XCTSkip("No test API token available")
        }

        app.launchArguments = [
            "-dev_serverURL", serverURL!,
            "-dev_apiToken", token,
            "-dev_skipAutoLogin", "true",
        ]
        app.launch()

        let addButton = app.buttons["instance-add-button"]
        XCTAssertTrue(addButton.waitForExistence(timeout: 15), "Instance list did not appear")
        addButton.tap()

        // Switch to API Token auth mode
        let tokenSegment = app.buttons["API Token"]
        XCTAssertTrue(tokenSegment.waitForExistence(timeout: 5), "API Token segment not found")
        tokenSegment.tap()

        // Scroll form to reveal login button
        app.swipeUp()

        let loginButton = app.buttons["login-button"]
        XCTAssertTrue(loginButton.waitForExistence(timeout: 5))
        loginButton.tap()

        // Verify we land on HomeView
        let reposTab = app.tabBars.buttons["Repositories"]
        XCTAssertTrue(reposTab.waitForExistence(timeout: 15), "HomeView did not appear after token login")
    }

    // MARK: - Token Login Bad Token

    @MainActor
    func testLoginWithBadToken() throws {
        app.launchArguments = [
            "-dev_serverURL", serverURL!,
            "-dev_apiToken", "invalid_token_value",
            "-dev_skipAutoLogin", "true",
        ]
        app.launch()

        let addButton = app.buttons["instance-add-button"]
        XCTAssertTrue(addButton.waitForExistence(timeout: 15), "Instance list did not appear")
        addButton.tap()

        // Switch to API Token auth mode
        let tokenSegment = app.buttons["API Token"]
        XCTAssertTrue(tokenSegment.waitForExistence(timeout: 5), "API Token segment not found")
        tokenSegment.tap()

        // Scroll form to reveal login button
        app.swipeUp()

        let loginButton = app.buttons["login-button"]
        XCTAssertTrue(loginButton.waitForExistence(timeout: 5))
        loginButton.tap()

        let alert = app.alerts["Login Failed"]
        XCTAssertTrue(alert.waitForExistence(timeout: 10))
    }
}
