//
//  SpanishLocalizationTests.swift
//  Tests iOS
//
//  TC-137: Spanish localization displays correctly.
//

import XCTest

final class SpanishLocalizationTests: BaseUITestCase {
    override var seedFixture: String? { "week_of_moods" }
    override var bypassSubscription: Bool { true }

    override func setUp() {
        // Do NOT call super — we need custom language launch args
        continueAfterFailure = false

        let application = XCUIApplication()
        let args: [String] = [
            "--ui-testing", "--disable-animations",
            "--reset-state",
            "--bypass-subscription",
            "--skip-onboarding",
            "-AppleLanguages", "(es)",
            "-AppleLocale", "es_ES"
        ]
        application.launchArguments = args
        application.launchEnvironment = ["UI_TEST_FIXTURE": "week_of_moods"]
        application.launch()
        app = application
    }

    /// TC-137: Key Spanish strings appear when launched in Spanish locale.
    func testSpanishLocale_DisplaysSpanishStrings() {
        // Day tab should load with data
        let tabBar = app.tabBars.firstMatch
        XCTAssertTrue(tabBar.waitForExistence(timeout: 5), "Tab bar should exist")

        captureScreenshot(name: "spanish_day_tab")

        // Tap the Settings tab by its Spanish label "Ajustes"
        let settingsTabButton = app.tabBars.buttons["Ajustes"]
        XCTAssertTrue(
            settingsTabButton.waitForExistence(timeout: 5),
            "Settings tab should show Spanish label 'Ajustes'"
        )
        settingsTabButton.tap()

        // Verify Settings header is visible via accessibility ID
        let settingsHeader = app.element(UITestID.Settings.header)
        XCTAssertTrue(
            settingsHeader.waitForExistence(timeout: 5),
            "Settings header should be visible"
        )

        // Verify Spanish text "Ajustes" appears as a static text on screen
        let ajustesText = app.staticTexts.matching(
            NSPredicate(format: "label == %@", "Ajustes")
        ).firstMatch

        XCTAssertTrue(
            ajustesText.waitForExistence(timeout: 5),
            "Settings should display 'Ajustes' in Spanish locale"
        )

        captureScreenshot(name: "spanish_settings_tab")
    }
}
