//
//  MonthViewInteractionTests.swift
//  Tests iOS
//
//  Month view interaction tests — tapping into month content.
//

import XCTest

final class MonthViewInteractionTests: BaseUITestCase {
    override var seedFixture: String? { "week_of_moods" }

    /// TC-030: Tap on month view content and verify interaction works without crash.
    func testMonthView_TapContent_NoCrash() {
        let tabBar = TabBarScreen(app: app)

        // 1. Navigate to Month tab
        tabBar.tapMonth()
        XCTAssertTrue(tabBar.monthTab.isSelected, "Month tab should be selected")

        // 2. Wait for month grid content to load
        let monthGrid = app.element(UITestID.Month.grid)
        let scrollView = app.scrollViews.firstMatch

        // Either the month_grid identifier or a scroll view should be present
        let contentLoaded = monthGrid.waitForExistence(timeout: 5) ||
                            scrollView.waitForExistence(timeout: 5)
        XCTAssertTrue(contentLoaded, "Month view should have loaded content")

        captureScreenshot(name: "month_view_before_tap")

        // 3. Tap on the month view content (first cell/card in the grid)
        //    Try the month_grid element first; fall back to tapping the scroll view content
        if monthGrid.exists && monthGrid.isHittable {
            monthGrid.tap()
        } else if scrollView.exists && scrollView.isHittable {
            // Tap near the center of the scroll view to hit a month card
            scrollView.tap()
        }

        // 4. Verify the app did not crash — the tab bar should still be accessible
        XCTAssertTrue(
            tabBar.monthTab.waitForExistence(timeout: 5),
            "App should remain stable after tapping month content"
        )

        // 5. Check if any detail/navigation occurred (look for navigation bar or content change)
        //    Month view may show a detail view or popover depending on the card tapped
        let navBar = app.navigationBars.firstMatch
        let detailAppeared = navBar.waitForExistence(timeout: 3)

        if detailAppeared {
            captureScreenshot(name: "month_detail_view")
        } else {
            // No navigation occurred, which is also valid — the main check is no crash
            captureScreenshot(name: "month_view_after_tap")
        }
    }

    /// Navigate to Month tab with data, scroll down, and verify no crash.
    func testMonthView_Scroll_NoCrash() {
        let tabBar = TabBarScreen(app: app)

        // Navigate to Month tab
        tabBar.tapMonth()
        XCTAssertTrue(tabBar.monthTab.isSelected, "Month tab should be selected")

        // Wait for content to load
        let scrollView = app.scrollViews.firstMatch
        guard scrollView.waitForExistence(timeout: 5) else {
            // If no scroll view, the month view may use a different layout — verify no crash
            XCTAssertTrue(tabBar.monthTab.exists, "App should not crash on month view")
            return
        }

        // Scroll down and up
        scrollView.swipeUp()
        scrollView.swipeDown()

        // Verify the app is still stable
        XCTAssertTrue(
            tabBar.monthTab.waitForExistence(timeout: 3),
            "App should remain stable after scrolling month view"
        )

        captureScreenshot(name: "month_view_after_scroll")
    }
}
