import XCTest

final class StemChangeToggleTests: XCTestCase {

    override func setUpWithError() throws {
        continueAfterFailure = false
    }

    func testStemChangeConjugationToggle() throws {
        let app = XCUIApplication()
        app.launchArguments += ["-onboardingComplete", "YES"]
        app.launch()

        // Course → LanGo Beginner I → Week 4 → E-IE stem-changing verbs
        app.tabBars.buttons["Course"].tap()

        // Locate the E-IE deck row. Deck titles appear as static text / button.
        // Scroll until visible, then tap.
        let deckPredicate = NSPredicate(format: "label CONTAINS[c] 'E-IE stem changing verbs' AND NOT label CONTAINS[c] 'REVÉS'")
        let deckRow = app.buttons.matching(deckPredicate).firstMatch

        let listRef = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.85))
        let topRef = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.10))
        for _ in 0..<12 {
            if deckRow.exists && deckRow.isHittable { break }
            listRef.press(forDuration: 0.1, thenDragTo: topRef)
        }
        XCTAssertTrue(deckRow.waitForExistence(timeout: 3), "E-IE deck row missing")
        deckRow.tap()

        attach(app, name: "01-deck-top")

        // Tap "Show conjugation" on the first card
        let showBtn = app.buttons.matching(NSPredicate(format: "label BEGINSWITH 'Show conjugation'")).firstMatch
        XCTAssertTrue(showBtn.waitForExistence(timeout: 3), "Show conjugation button missing")
        showBtn.tap()

        // Wait for the conjugation rows + animation to settle.
        let yoLabel = app.staticTexts["yo"].firstMatch
        XCTAssertTrue(yoLabel.waitForExistence(timeout: 3), "yo row not rendered")
        // Give the transition time to complete before snapshotting.
        Thread.sleep(forTimeInterval: 0.6)
        attach(app, name: "02-conjugation-open")

        // Also confirm all expected person labels are rendered.
        for person in ["yo", "tú", "nosotros"] {
            XCTAssertTrue(
                app.staticTexts[person].firstMatch.exists,
                "missing conjugation row for \(person)"
            )
        }

        // Tap again to hide
        let hideBtn = app.buttons.matching(NSPredicate(format: "label BEGINSWITH 'Hide conjugation'")).firstMatch
        XCTAssertTrue(hideBtn.waitForExistence(timeout: 2))
        hideBtn.tap()
    }

    private func attach(_ app: XCUIApplication, name: String) {
        let s = app.screenshot()
        let a = XCTAttachment(screenshot: s)
        a.name = name
        a.lifetime = .keepAlways
        add(a)
    }
}
