//
//  NotesTests.swift
//  Tests iOS
//
//  Notes add/edit and emoji support tests.
//

import XCTest

final class NotesTests: BaseUITestCase {
    override var seedFixture: String? { "single_mood" }

    /// TC-026 / TC-132: Add a note to an existing entry.
    func testAddNote_ToExistingEntry() {
        guard app.firstEntryRow.waitForExistence(timeout: 8) else {
            XCTFail("No entry row found")
            return
        }
        app.firstEntryRow.tapWhenReady()

        let detailScreen = EntryDetailScreen(app: app)
        detailScreen.assertVisible()

        // Tap the note area to open the note editor
        let noteArea = app.element(UITestID.EntryDetail.noteArea)
        if !noteArea.waitForExistence(timeout: 3) {
            // Try the note button instead
            let noteButton = app.element(UITestID.EntryDetail.noteButton)
            guard noteButton.waitForExistence(timeout: 3) else {
                XCTFail("Neither note area nor note button found")
                return
            }
            noteButton.tapWhenReady()
        } else {
            noteArea.tapWhenReady()
        }

        let noteEditor = NoteEditorScreen(app: app)
        noteEditor.assertVisible()

        // Type a note
        noteEditor.clearAndTypeNote("Had a great day today!")

        captureScreenshot(name: "note_typed")

        // Save the note
        noteEditor.save()

        // Note editor should dismiss
        noteEditor.assertDismissed()

        // Verify the note text is visible in the detail view
        let noteText = app.staticTexts.matching(NSPredicate(format: "label CONTAINS %@", "Had a great day today!")).firstMatch
        XCTAssertTrue(
            noteText.waitForExistence(timeout: 5),
            "Saved note text should be visible in entry detail"
        )

        captureScreenshot(name: "note_saved")

        // Dismiss detail
        detailScreen.dismiss()
        detailScreen.assertDismissed()
    }

    /// TC-135: Add a note with emoji and special characters.
    func testAddNote_WithEmoji() {
        guard app.firstEntryRow.waitForExistence(timeout: 8) else {
            XCTFail("No entry row found")
            return
        }
        app.firstEntryRow.tapWhenReady()

        let detailScreen = EntryDetailScreen(app: app)
        detailScreen.assertVisible()

        // Open note editor
        let noteArea = app.element(UITestID.EntryDetail.noteArea)
        if noteArea.waitForExistence(timeout: 3) {
            noteArea.tapWhenReady()
        } else {
            let noteButton = app.element(UITestID.EntryDetail.noteButton)
            noteButton.tapWhenReady()
        }

        let noteEditor = NoteEditorScreen(app: app)
        noteEditor.assertVisible()

        // Type emoji text - note: XCUITest typeText supports Unicode
        noteEditor.clearAndTypeNote("Feeling amazing! #100")

        // Save
        noteEditor.save()
        noteEditor.assertDismissed()

        captureScreenshot(name: "note_with_special_chars")

        detailScreen.dismiss()
        detailScreen.assertDismissed()
    }
}
