import { test, expect } from './helpers/fixtures.js'

test.describe('Board page', () => {
  test('page loads for authenticated member', async ({ memberPage }) => {
    await memberPage.goto('/board')
    await expect(memberPage.getByRole('heading', { name: 'Board' })).toBeVisible({ timeout: 15000 })
    await expect(memberPage.getByRole('button', { name: '+ New Post' }).first()).toBeVisible()
  })

  test('clicking New Post reveals the form', async ({ memberPage }) => {
    await memberPage.goto('/board')
    await expect(memberPage.getByRole('button', { name: '+ New Post' }).first()).toBeVisible({
      timeout: 15000,
    })

    await memberPage.getByRole('button', { name: '+ New Post' }).first().click()

    await expect(memberPage.getByRole('heading', { name: 'New post' })).toBeVisible()
    await expect(memberPage.locator('#post-title')).toBeVisible()
    await expect(memberPage.locator('#post-seeking')).toBeVisible()
  })

  test('tags drawer toggles open and closed', async ({ memberPage }) => {
    await memberPage.goto('/board')
    await expect(memberPage.getByRole('heading', { name: 'Board' })).toBeVisible({ timeout: 15000 })

    const drawerToggle = memberPage.getByRole('button', { name: /^Tags\.\.\./ })
    // Drawer toggle only appears if cooperative tags exist — skip quietly if not
    if (!(await drawerToggle.isVisible().catch(() => false))) {
      test.skip(true, 'No cooperative tags seeded in this environment')
      return
    }

    await drawerToggle.click()
    await expect(memberPage.getByText('Filter:')).toBeVisible()

    await drawerToggle.click()
    await expect(memberPage.getByText('Filter:')).not.toBeVisible()
  })

  test('create, edit, and delete own post', async ({ memberPage }) => {
    await memberPage.goto('/board')
    await expect(memberPage.getByRole('button', { name: '+ New Post' }).first()).toBeVisible({
      timeout: 15000,
    })

    const uniqueSuffix = Date.now().toString().slice(-6)
    const originalTitle = `E2E test post ${uniqueSuffix}`
    const editedTitle = `E2E test post edited ${uniqueSuffix}`

    // --- Create ---
    await memberPage.getByRole('button', { name: '+ New Post' }).first().click()
    await expect(memberPage.getByRole('heading', { name: 'New post' })).toBeVisible()

    await memberPage.locator('#post-title').fill(originalTitle)
    await memberPage.locator('#post-seeking').fill('Playwright test seeking text')

    await memberPage.getByRole('button', { name: 'Post' }).click()

    await expect(memberPage.getByRole('heading', { name: originalTitle })).toBeVisible({
      timeout: 10000,
    })

    // --- Edit ---
    // Find the post card containing our title, then click its Edit button
    const postCard = memberPage.locator('article.board-post', { hasText: originalTitle })
    await postCard.getByRole('button', { name: 'Edit' }).click()

    await expect(memberPage.getByRole('heading', { name: 'Edit post' })).toBeVisible()
    const titleInput = memberPage.locator('#post-title')
    await titleInput.fill(editedTitle)
    await memberPage.getByRole('button', { name: 'Save changes' }).click()

    await expect(memberPage.getByRole('heading', { name: editedTitle })).toBeVisible({
      timeout: 10000,
    })

    // --- Delete (confirm dialog) ---
    memberPage.once('dialog', (dialog) => dialog.accept())
    const editedCard = memberPage.locator('article.board-post', { hasText: editedTitle })
    await editedCard.getByRole('button', { name: 'Delete' }).click()

    await expect(memberPage.getByRole('heading', { name: editedTitle })).not.toBeVisible({
      timeout: 10000,
    })
  })
})
