//
//  DayViewGroupingTests.swift
//  Tests iOS
//
//  Day view section header grouping tests.
//

import XCTest

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

    /// TC-019: Entries are grouped by year/month section headers.
    func testEntries_GroupedBySectionHeaders() {
        // 1. Wait for entry list to load with seeded data
        let firstEntry = app.firstEntryRow
        XCTAssertTrue(
            firstEntry.waitForExistence(timeout: 5),
            "Entry rows should exist with week_of_moods fixture"
        )

        // 2. Verify at least one section header exists
        let anySectionHeader = app.descendants(matching: .any)
            .matching(NSPredicate(format: "identifier BEGINSWITH %@", UITestID.Day.sectionPrefix))
            .firstMatch
        XCTAssertTrue(
            anySectionHeader.waitForExistence(timeout: 5),
            "At least one day_section_ header should exist"
        )

        // 3. The week_of_moods fixture contains entries in the current month.
        //    Verify the section header for the current month/year exists.
        let now = Date()
        let calendar = Calendar.current
        let month = calendar.component(.month, from: now)
        let year = calendar.component(.year, from: now)

        let expectedHeaderID = "day_section_\(month)_\(year)"
        let currentMonthHeader = app.descendants(matching: .any)
            .matching(identifier: expectedHeaderID)
            .firstMatch
        XCTAssertTrue(
            currentMonthHeader.waitForExistence(timeout: 5),
            "Section header '\(expectedHeaderID)' should exist for current month"
        )

        captureScreenshot(name: "day_view_section_headers")
    }
}
