//
//  TrialBannerTests.swift
//  Tests iOS
//
//  TC-033: Trial warning banner shown.
//  TC-076: Fresh install starts 30-day trial.
//  TC-080: Trial banner hidden with bypass (DEBUG).
//

import XCTest

/// Tests that verify trial banner visibility based on subscription state.
final class TrialBannerTests: BaseUITestCase {
    override var seedFixture: String? { "single_mood" }

    /// TC-076: On fresh install, Settings shows an upgrade banner (indicating trial is active).
    func testFreshInstall_ShowsTrialBanner() {
        let tabBar = TabBarScreen(app: app)
        let settingsScreen = tabBar.tapSettings()
        settingsScreen.assertVisible()

        // With default settings (bypassSubscription = true), the banner is hidden.
        // We need to launch without bypass to see the banner.
        // Re-launch with bypass disabled.
        app.terminate()

        let freshApp = XCUIApplication()
        var args = ["--ui-testing", "--reset-state", "--disable-animations", "--skip-onboarding",
                    "-AppleLanguages", "(en)", "-AppleLocale", "en_US"]
        // Do NOT add --bypass-subscription
        freshApp.launchArguments = args
        if let fixture = seedFixture {
            freshApp.launchEnvironment = ["UI_TEST_FIXTURE": fixture]
        }
        freshApp.launch()
        app = freshApp

        // Navigate to Settings
        let freshTabBar = TabBarScreen(app: app)
        let freshSettings = freshTabBar.tapSettings()
        freshSettings.assertVisible()

        // Upgrade banner should be visible (trial is active, not bypassed)
        let upgradeBanner = freshSettings.upgradeBanner
        let bannerVisible = upgradeBanner.waitForExistence(timeout: 5)

        captureScreenshot(name: "trial_banner_visible")

        XCTAssertTrue(
            bannerVisible,
            "Upgrade banner should be visible on fresh install (trial active, no bypass)"
        )
    }

    /// TC-080: With --bypass-subscription, the trial banner is hidden.
    func testTrialBanner_HiddenWithBypass() {
        // Default BaseUITestCase has bypassSubscription = true
        let tabBar = TabBarScreen(app: app)
        let settingsScreen = tabBar.tapSettings()
        settingsScreen.assertVisible()

        // Upgrade banner should NOT be visible
        settingsScreen.assertUpgradeBannerHidden()

        captureScreenshot(name: "trial_banner_hidden_bypass")
    }
}

/// Separate test class for trial warning banner (TC-033) using expired trial state.
final class TrialWarningBannerTests: BaseUITestCase {
    override var seedFixture: String? { "single_mood" }
    override var bypassSubscription: Bool { false }
    override var expireTrial: Bool { false }

    /// TC-033: When trial is active (not expired, not bypassed), Settings shows a warning banner.
    func testTrialWarningBanner_Shown() {
        let tabBar = TabBarScreen(app: app)
        let settingsScreen = tabBar.tapSettings()
        settingsScreen.assertVisible()

        // The upgrade banner should be visible
        let upgradeBanner = settingsScreen.upgradeBanner
        let visible = upgradeBanner.waitForExistence(timeout: 5)

        captureScreenshot(name: "trial_warning_banner")

        XCTAssertTrue(
            visible,
            "Trial warning banner should be visible when trial is active and subscription not bypassed"
        )
    }
}
