import XCTest

final class StabilityTests: BaseUITestCase {
    func testP001_RapidOnboardingNavigationDoesNotCrash() {
        for _ in 0..<3 {
            let welcome = OnboardingWelcomeScreen(app: app)
            welcome.waitForLoad(timeout: defaultTimeout)
            welcome.tapStartFresh()

            let valueProps = OnboardingValuePropsScreen(app: app)
            valueProps.waitForLoad(timeout: defaultTimeout)
            valueProps.tapBack()

            welcome.waitForLoad(timeout: defaultTimeout)
        }
    }

    func testP002_RepeatedForwardNavigationRemainsResponsive() {
        for index in 0..<3 {
            let welcome = OnboardingWelcomeScreen(app: app)
            welcome.waitForLoad(timeout: defaultTimeout)
            welcome.tapStartFresh()

            let valueProps = OnboardingValuePropsScreen(app: app)
            valueProps.waitForLoad(timeout: defaultTimeout)
            valueProps.tapContinue()

            let nameResidence = OnboardingNameResidenceScreen(app: app)
            nameResidence.waitForLoad(timeout: defaultTimeout)
            nameResidence.enterResidenceName("Stress Home \(index)")
            nameResidence.tapBack()

            valueProps.waitForLoad(timeout: defaultTimeout)
            valueProps.tapBack()

            welcome.waitForLoad(timeout: defaultTimeout)
        }
    }

    func testP003_RapidDoubleTapOnValuePropsContinueLandsOnNameResidence() {
        let welcome = OnboardingWelcomeScreen(app: app)
        welcome.waitForLoad(timeout: defaultTimeout)
        welcome.tapStartFresh()

        let valueProps = OnboardingValuePropsScreen(app: app)
        valueProps.waitForLoad(timeout: defaultTimeout)

        let continueButton = app.buttons[UITestID.Onboarding.valuePropsNextButton]
        continueButton.waitUntilHittable(timeout: defaultTimeout).tap()
        if continueButton.exists && continueButton.isHittable {
            continueButton.tap()
        }

        let nameResidence = OnboardingNameResidenceScreen(app: app)
        nameResidence.waitForLoad(timeout: defaultTimeout)
    }

    // MARK: - Additional Stability Coverage

    func testP004_StartFreshThenBackToWelcomeThenJoinExistingDoesNotCorruptState() {
        let welcome = OnboardingWelcomeScreen(app: app)
        welcome.waitForLoad(timeout: defaultTimeout)

        // Start fresh path
        welcome.tapStartFresh()
        let valueProps = OnboardingValuePropsScreen(app: app)
        valueProps.waitForLoad(timeout: defaultTimeout)

        // Go back to welcome
        valueProps.tapBack()
        welcome.waitForLoad(timeout: defaultTimeout)

        // Switch to join existing path
        welcome.tapJoinExisting()
        let createAccount = OnboardingCreateAccountScreen(app: app)
        createAccount.waitForLoad(timeout: defaultTimeout)
    }

    func testP005_RepeatedLoginNavigationRemainsStable() {
        for _ in 0..<3 {
            let login = TestFlows.navigateToLoginFromOnboarding(app: app)
            login.waitForLoad(timeout: defaultTimeout)

            // Dismiss login (swipe down or navigate back)
            let backButton = app.descendants(matching: .any).matching(identifier: UITestID.Onboarding.backButton).firstMatch
            if backButton.waitForExistence(timeout: defaultTimeout) && backButton.isHittable {
                backButton.forceTap()
            } else {
                // Try swipe down to dismiss sheet
                app.swipeDown()
            }

            // Should return to onboarding
            let welcome = OnboardingWelcomeScreen(app: app)
            welcome.waitForLoad(timeout: defaultTimeout)
        }
    }

}
