//
//  NoteEditTests.swift
//  Tests iOS
//
//  TC-133: Edit an existing note.
//  TC-134: Long note (>1000 characters).
//

import XCTest

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

    // MARK: - Helpers

    /// Opens the note editor for the first entry and types the given text.
    /// Returns the entry detail and note editor screens for further assertions.
    private func addNote(_ text: String) -> (detail: EntryDetailScreen, editor: NoteEditorScreen) {
        guard app.firstEntryRow.waitForExistence(timeout: 8) else {
            XCTFail("No entry row found")
            return (EntryDetailScreen(app: app), NoteEditorScreen(app: app))
        }
        app.firstEntryRow.tapWhenReady()

        let detail = EntryDetailScreen(app: app)
        detail.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 editor = NoteEditorScreen(app: app)
        editor.assertVisible()
        editor.clearAndTypeNote(text)
        editor.save()
        editor.assertDismissed()

        return (detail, editor)
    }

    /// Re-opens the note editor from the current entry detail view.
    private func reopenNoteEditor() -> NoteEditorScreen {
        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 editor = NoteEditorScreen(app: app)
        editor.assertVisible()
        return editor
    }

    // MARK: - Tests

    /// TC-133: Edit an existing note — add note, reopen, change text, verify new text.
    func testEditNote_ExistingEntry() {
        // Step 1: Add initial note
        let (detail, _) = addNote("Original note text")

        // Verify initial note is visible
        let originalText = app.staticTexts.matching(
            NSPredicate(format: "label CONTAINS %@", "Original note text")
        ).firstMatch
        XCTAssertTrue(
            originalText.waitForExistence(timeout: 5),
            "Original note should be visible"
        )

        captureScreenshot(name: "note_original")

        // Step 2: Reopen and edit the note
        let editor = reopenNoteEditor()
        editor.clearAndTypeNote("Updated note text")
        editor.save()
        editor.assertDismissed()

        // Step 3: Verify edited note is shown
        let updatedText = app.staticTexts.matching(
            NSPredicate(format: "label CONTAINS %@", "Updated note text")
        ).firstMatch
        XCTAssertTrue(
            updatedText.waitForExistence(timeout: 5),
            "Updated note text should be visible after editing"
        )

        captureScreenshot(name: "note_edited")

        detail.dismiss()
        detail.assertDismissed()
    }

    /// TC-134: Add a long note (>1000 characters).
    func testLongNote_Over1000Characters() {
        // Generate a long string > 1000 chars
        let longText = String(repeating: "This is a test note. ", count: 55) // ~1155 chars

        // Add the long note
        let (detail, _) = addNote(longText)

        // Verify some portion of the note is visible
        let noteSnippet = app.staticTexts.matching(
            NSPredicate(format: "label CONTAINS %@", "This is a test note")
        ).firstMatch
        XCTAssertTrue(
            noteSnippet.waitForExistence(timeout: 5),
            "Long note text should be visible after saving"
        )

        captureScreenshot(name: "note_long_saved")

        detail.dismiss()
        detail.assertDismissed()
    }
}
