//
//  AppThemeTests.swift
//  Tests iOS
//
//  App theme tests: browse themes sheet, verify all 12 theme cards exist,
//  and apply a theme without crashing.
//  TC-070
//

import XCTest

final class AppThemeTests: BaseUITestCase {
    override var seedFixture: String? { "single_mood" }
    override var bypassSubscription: Bool { true }

    /// All 12 app theme names (must match the accessibility IDs: apptheme_card_{lowercased name}).
    private let allThemes = [
        "Zen Garden", "Synthwave", "Celestial", "Editorial",
        "Mixtape", "Bloom", "Heartfelt", "Minimal",
        "Luxe", "Forecast", "Playful", "Journal"
    ]

    /// TC-070: Open Browse Themes sheet and verify all 12 theme cards exist.
    func testBrowseThemes_AllCardsExist() {
        let tabBar = TabBarScreen(app: app)
        let settingsScreen = tabBar.tapSettings()
        settingsScreen.assertVisible()
        settingsScreen.tapCustomizeTab()

        let customizeScreen = CustomizeScreen(app: app)
        XCTAssertTrue(customizeScreen.openThemePicker(), "Themes sheet should appear with theme cards")

        // Verify all 12 theme cards are accessible (some may require scrolling)
        for theme in allThemes {
            let card = customizeScreen.appThemeCard(named: theme)
            if !card.exists { _ = app.swipeUntilExists(card, direction: .up, maxSwipes: 6) }
            XCTAssertTrue(
                card.waitForExistence(timeout: 3),
                "Theme card '\(theme)' should exist in the Browse Themes sheet"
            )
        }

        captureScreenshot(name: "browse_themes_all_cards")
    }

    /// TC-070: Apply a representative set of themes and verify no crash.
    func testApplyThemes_NoCrash() {
        let tabBar = TabBarScreen(app: app)
        let settingsScreen = tabBar.tapSettings()
        settingsScreen.assertVisible()
        settingsScreen.tapCustomizeTab()

        let customizeScreen = CustomizeScreen(app: app)
        XCTAssertTrue(customizeScreen.openThemePicker(), "Browse Themes sheet should open")

        // Tap a representative sample of themes: first, middle, last
        let sampled = ["Zen Garden", "Heartfelt", "Journal"]
        for theme in sampled {
            let card = customizeScreen.appThemeCard(named: theme)
            if !card.exists { _ = app.swipeUntilExists(card, direction: .up, maxSwipes: 6) }
            if card.waitForExistence(timeout: 3) {
                card.tapWhenReady(timeout: 3)

                // Apply theme via stable accessibility id.
                let applyButton = app.element(UITestID.Customize.previewApplyButton)
                if applyButton.waitForExistence(timeout: 3) {
                    applyButton.tapWhenReady()
                } else {
                    let cancelButton = app.element(UITestID.Customize.previewCancelButton)
                    if cancelButton.waitForExistence(timeout: 2) {
                        cancelButton.tapWhenReady()
                    }
                }
            }
        }

        captureScreenshot(name: "themes_applied")

        // Dismiss the themes sheet by swiping down or tapping Done
        let doneButton = app.element(UITestID.Customize.pickerDoneButton)
        if doneButton.waitForExistence(timeout: 2) {
            doneButton.tapWhenReady()
        } else {
            // Swipe down to dismiss the sheet
            app.swipeDown()
        }

        // Wait for sheet dismissal — verify the sheet is actually gone
        // by checking that the tab bar is accessible again
        let tabBarElement = app.tabBars.firstMatch
        if !tabBarElement.waitForExistence(timeout: 3) {
            // Sheet may still be visible — try dismissing again
            app.swipeDown()
            _ = tabBarElement.waitForExistence(timeout: 3)
        }

        // Navigate to Day tab and verify no crash — entry row should still exist
        tabBar.tapDay()
        assertDayContentVisible(timeout: 10)

        captureScreenshot(name: "day_view_after_theme_change")
    }
}
