//
//  InsightsPullToRefreshTests.swift
//  Tests iOS
//
//  TC-047: Pull to refresh on Insights tab.
//

import XCTest

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

    /// TC-047: Pull-to-refresh gesture on Insights tab does not crash and UI remains functional.
    func testInsights_PullToRefresh_NoLayoutCrash() {
        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_before_refresh")

        // Perform pull-to-refresh gesture (drag from top area downward)
        let start = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.3))
        let end = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.8))
        start.press(forDuration: 0.1, thenDragTo: end)

        // Wait for refresh to settle
        _ = app.waitForExistence(timeout: 3)

        captureScreenshot(name: "insights_after_refresh")

        // Verify UI is still functional — header should still be there
        XCTAssertTrue(
            header.waitForExistence(timeout: 5),
            "Insights header should still be visible after pull-to-refresh"
        )

        // Verify sections are still present
        let monthTitle = app.staticTexts["This Month"].firstMatch
        XCTAssertTrue(
            monthTitle.waitForExistence(timeout: 5),
            "This Month section should still be visible after pull-to-refresh"
        )
    }
}
