/**
 * E2E tests for Bitable file CRUD flow (U3 / R1).
 *
 * Flow: login → /bitable → create file → open file → create table →
 *       verify default fields → delete file.
 *
 * Requires: running backend with PostgreSQL (bitable schema initialized).
 * Skips gracefully if backend is unreachable.
 */

import { test, expect, type Page } from '@playwright/test'
import { TEST_USER, clearAuth, waitForServer } from './helpers'

async function loginAndNavigateToBitable(page: Page): Promise<void> {
  await page.goto('/login')
  await clearAuth(page)
  await page.getByPlaceholder('请输入用户名').fill(TEST_USER.username)
  await page.getByPlaceholder('请输入密码').fill(TEST_USER.password)
  await page.getByRole('button', { name: /登\s*录/ }).click()
  await expect(page).toHaveURL(/\/agent/, { timeout: 15_000 })
  await page.getByRole('button', { name: '多维表格' }).click()
  await expect(page).toHaveURL(/\/bitable$/, { timeout: 15_000 })
}

test.describe('Bitable File Flow E2E', () => {
  test.beforeAll(async () => {
    // Skip entire suite if backend is not running.
    try {
      await waitForServer(undefined, 5_000)
    } catch {
      test.skip(true, 'Backend not running — skipping bitable file flow E2E')
    }
  })

  test('F1: create a new bitable file', async ({ page }) => {
    await loginAndNavigateToBitable(page)

    // Click "新建文件" button
    await page.getByRole('button', { name: /新建文件/ }).click()

    // Fill the create modal
    await expect(page.getByText('新建多维表格文件')).toBeVisible({ timeout: 5_000 })
    await page.getByPlaceholder('请输入文件名').fill('E2E测试文件')
    await page.getByRole('button', { name: /确\s*定/ }).click()

    // Should navigate to the file detail page
    await expect(page).toHaveURL(/\/bitable\/[^/]+/, { timeout: 10_000 })
    await expect(page.locator('.bitable-file-detail-view')).toBeVisible()
    await expect(page.locator('.bitable-file-detail-view__title')).toContainText('E2E测试文件')
  })

  test('F2: create a table inside a file', async ({ page }) => {
    await loginAndNavigateToBitable(page)

    // Create a file first
    await page.getByRole('button', { name: /新建文件/ }).click()
    await page.getByPlaceholder('请输入文件名').fill('E2E表格测试')
    await page.getByRole('button', { name: /确\s*定/ }).click()
    await expect(page).toHaveURL(/\/bitable\/[^/]+/, { timeout: 10_000 })

    // Click + in the sidebar to create a table
    await page.locator('.table-view-list__header .ant-btn').click()

    // Fill table create modal
    await expect(page.getByText('新建数据表')).toBeVisible({ timeout: 5_000 })
    await page.getByPlaceholder('请输入表名').fill('测试表')
    await page.getByRole('button', { name: /确\s*定/ }).click()

    // Table should appear in sidebar and grid should render
    await expect(page.locator('.bitable-file-detail-view__table-name')).toContainText('测试表', {
      timeout: 10_000,
    })
  })

  test('F3: default fields are created with new table', async ({ page }) => {
    await loginAndNavigateToBitable(page)

    // Create file + table
    await page.getByRole('button', { name: /新建文件/ }).click()
    await page.getByPlaceholder('请输入文件名').fill('E2E默认字段测试')
    await page.getByRole('button', { name: /确\s*定/ }).click()
    await expect(page).toHaveURL(/\/bitable\/[^/]+/, { timeout: 10_000 })

    await page.locator('.table-view-list__header .ant-btn').click()
    await page.getByPlaceholder('请输入表名').fill('默认字段表')
    await page.getByRole('button', { name: /确\s*定/ }).click()

    // Wait for grid to render, check field count includes 5 default fields
    await expect(page.locator('.bitable-file-detail-view__field-count')).toContainText(/5\s*个字段/, {
      timeout: 10_000,
    })
  })

  test('F4: delete a file via context menu', async ({ page }) => {
    await loginAndNavigateToBitable(page)

    // Create a file to delete
    await page.getByRole('button', { name: /新建文件/ }).click()
    await page.getByPlaceholder('请输入文件名').fill('E2E删除测试')
    await page.getByRole('button', { name: /确\s*定/ }).click()
    await expect(page).toHaveURL(/\/bitable\/[^/]+/, { timeout: 10_000 })

    // Go back to file list
    await page.locator('.bitable-file-detail-view__topbar-left .ant-btn').click()
    await expect(page).toHaveURL(/\/bitable$/, { timeout: 10_000 })

    // Right-click the file card to open context menu
    const card = page.locator('.file-card').first()
    await card.click({ button: 'right' })

    // Click delete in context menu
    await page.getByText('删除').click()

    // Confirm deletion
    await page.getByRole('button', { name: /删\s*除/ }).click()

    // File should be removed from the list
    await expect(page.locator('.file-card')).toHaveCount(0, { timeout: 10_000 })
  })
})
