//
//  InsightsCollapseTests.swift
//  Tests iOS
//
//  TC-046: Collapse/expand insight sections.
//

import XCTest

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

    /// TC-046: Tapping a section header collapses/expands that section.
    func testInsights_CollapseExpandSections() {
        let tabBar = TabBarScreen(app: app)
        tabBar.tapInsights()

        // Verify Insights header loads
        let header = app.element(UITestID.Insights.header)
        XCTAssertTrue(
            header.waitForExistence(timeout: 8),
            "Insights header should be visible"
        )

        captureScreenshot(name: "insights_initial")

        // Find the "This Month" section header text and tap to collapse
        // Note: the text is inside a Button, so we use coordinate tap fallback
        let monthTitle = app.staticTexts["This Month"].firstMatch
        XCTAssertTrue(
            monthTitle.waitForExistence(timeout: 5),
            "This Month section title should exist"
        )

        monthTitle.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()

        // Brief wait for animation
        _ = app.waitForExistence(timeout: 1)

        captureScreenshot(name: "insights_month_collapsed")

        // Tap again to expand
        monthTitle.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()

        _ = app.waitForExistence(timeout: 1)

        captureScreenshot(name: "insights_month_expanded")

        // Also test "This Year" section
        let yearTitle = app.staticTexts["This Year"].firstMatch
        if yearTitle.waitForExistence(timeout: 3) {
            yearTitle.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
            _ = app.waitForExistence(timeout: 1)
            captureScreenshot(name: "insights_year_collapsed")
        }
    }
}
