
import { test, expect } from '@playwright/test'

test.describe('Recent Notes Dismissal Bug', () => {
    test('should not replace dismissed note immediately', async ({ page }) => {
        // 1. Create 4 notes to ensure we have enough to fill the list (limit is 3) + 1 extra
        await page.goto('/')
        await page.waitForLoadState('networkidle')

        // Create 4 notes
        for (let i = 1; i <= 4; i++) {
            const noteInput = page.locator('[data-testid="note-input-textarea"]')
            if (await noteInput.isVisible()) {
                await noteInput.fill(`Recent Note Test ${i} - ${Date.now()}`)
                await page.locator('button[type="submit"]').click()
                // Wait for creation
                await page.waitForTimeout(500)
            } else {
                // If input not visible, click "New Note" or "Add Note" button first?
                // Assuming input is visible or toggleable. 
                // Let's try to just open it if needed.
                const addBtn = page.locator('button:has-text("New Note")').first()
                if (await addBtn.isVisible()) {
                    await addBtn.click()
                }
                await page.locator('[data-testid="note-input-textarea"]').fill(`Recent Note Test ${i} - ${Date.now()}`)
                await page.locator('button[type="submit"]').click()
                await page.waitForTimeout(500)
            }
        }

        // Refresh to update recent list
        await page.reload()
        await page.waitForLoadState('networkidle')

        const recentSection = page.locator('[data-testid="recent-notes-section"]')
        await expect(recentSection).toBeVisible()

        // Should see 3 notes
        const noteCards = recentSection.locator('[data-testid^="note-card-"]')
        await expect(noteCards).toHaveCount(3)

        // 2. Dismiss one note
        const firstNote = noteCards.first()
        // Hover to see the dismiss button
        await firstNote.hover()
        const dismissBtn = firstNote.locator('button[title*="Dismiss"], button[title*="Fermer"]') // Trying both EN and FR just in case

        // We might need to force click if hover doesn't work perfectly in test
        await dismissBtn.click({ force: true })

        // 3. Verify behavior
        // PRE-FIX: The list refreshes and shows 3 notes (the 4th one pops in)
        // POST-FIX: The list should show 2 notes

        // We expect 2 notes if the fix works. 
        // If the bug is present, this assertion might fail (it will see 3).
        // For reproduction, we might want to assert failure or just see what happens.
        // Let's assert the DESIRED behavior (2 notes).
        await expect(noteCards).toHaveCount(2)
    })
})
