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

import {
  MOCK_BASE_URL,
  getDebugState,
  patchAdminSiteSettings,
  resetMockState,
  waitForCommentsReady,
  waitForHomeInteractive,
  waitForSubscriptionPopupReady,
} from './helpers'

async function gotoPage(page: Page, url: string) {
  await page.goto(url, { waitUntil: 'domcontentloaded' })
}

test.beforeEach(async ({ request }) => {
  await resetMockState(request)
})

test('首页过滤、热门区和文章详情链路可用', async ({ page }) => {
  await gotoPage(page, '/')
  await waitForHomeInteractive(page)

  await expect(page.locator('#home-results-count')).toContainText(/条结果/)

  await page.locator('[data-home-category-filter="测试体系"]').click()
  await expect(page.locator('#home-active-category-text')).toHaveText('测试体系')

  await page.locator('[data-home-tag-filter="Playwright"]').click()
  await expect(page.locator('#home-active-tag-text')).toHaveText('Playwright')

  await page.locator('[data-home-popular-range="30d"]').click()
  await expect(page.locator('#home-stats-window-pill')).toHaveText('30d')

  await page.locator('a[href="/articles/playwright-regression-workflow"]').first().click()
  await expect(page).toHaveURL(/\/articles\/playwright-regression-workflow$/)
  await expect(page.getByRole('heading', { name: 'Playwright 回归工作流设计' })).toBeVisible()
  await expect(page.locator('.paragraph-comment-marker').first()).toBeVisible()

  await gotoPage(page, '/categories/frontend-engineering')
  await expect(page.getByRole('heading', { name: '前端工程' })).toBeVisible()
  await expect(page.getByText('Astro 终端博客信息架构实战')).toBeVisible()

  await gotoPage(page, '/tags/playwright')
  await expect(page.getByRole('heading', { name: 'Playwright', exact: true })).toBeVisible()
  await expect(page.getByText('Playwright 回归工作流设计')).toBeVisible()

  await gotoPage(page, '/reviews')
  await expect(page.getByText('《宇宙探索编辑部》')).toHaveCount(0)
  await gotoPage(page, '/reviews/4')
  await expect(page.getByRole('heading', { name: '评价不存在' })).toBeVisible()

  await gotoPage(page, '/reviews/1')
  await page.getByRole('link', { name: '#年度最佳' }).click()
  await expect(page).toHaveURL(/\/reviews\?tag=%E5%B9%B4%E5%BA%A6%E6%9C%80%E4%BD%B3$/)
  await expect(page.getByText('《漫长的季节》')).toBeVisible()

  await gotoPage(page, '/reviews/1')
  await page.getByRole('link', { name: '动画' }).click()
  await expect(page).toHaveURL(/\/reviews\?type=anime$/)
  await expect(page.locator('#reviews-subtitle')).toContainText('动画')
  await expect(page.getByText('《漫长的季节》')).toBeVisible()

  await gotoPage(page, '/reviews/1')
  await page.getByRole('link', { name: '已完成' }).click()
  await expect(page).toHaveURL(/\/reviews\?status=completed$/)
  await expect(page.locator('#reviews-subtitle')).toContainText('已完成')
  await expect(page.getByText('《漫长的季节》')).toBeVisible()
})

test('文章评论、搜索和 AI 问答链路可用', async ({ page, request }) => {
  await gotoPage(page, '/articles/astro-terminal-blog')
  await waitForCommentsReady(page)

  await page.locator('#toggle-comment-form').click()
  await expect(page.locator('#comment-form input[name="nickname"]')).toBeVisible()
  await page.locator('#comment-form input[name="nickname"]').fill('Playwright Visitor')
  await page.locator('#comment-form input[name="email"]').fill('visitor@example.com')
  await page.locator('#comment-form textarea[name="content"]').fill('这是一条来自回归测试的新评论。')
  await page.locator('#comment-form input[name="captchaAnswer"]').fill('7')
  await page.getByRole('button', { name: '提交' }).click()
  await expect(page.locator('#comment-message')).toContainText('提交')

  const commentState = await getDebugState(request)
  expect(commentState.comments.some((item: { author: string }) => item.author === 'Playwright Visitor')).toBeTruthy()

  await gotoPage(page, '/search?q=playwright')
  await expect(page.getByText('Playwright 回归工作流设计')).toBeVisible()

  await gotoPage(page, '/ask')
  await page.locator('#ai-question').fill('这个博客主要写什么内容？')
  await page.locator('#ai-submit').click()
  await expect(page.locator('#ai-answer')).toContainText('Playwright 回归工作流')
})

test('友链申请与订阅确认/偏好/退订链路可用', async ({ page, request }) => {
  await gotoPage(page, '/friends')

  await page.locator('input[name="siteName"]').fill('Playwright Friend')
  await page.locator('input[name="siteUrl"]').fill('https://playwright-friend.example')
  await page.locator('textarea[name="description"]').fill('回归测试用的友链申请。')
  await page.locator('label', { hasText: '[其他]' }).click()
  await page.locator('#has-reciprocal').check()
  await page.getByRole('button', { name: '提交申请' }).click()
  await expect(page.locator('#form-message')).toContainText('提交')

  const friendState = await getDebugState(request)
  expect(friendState.friend_links.some((item: { site_name: string }) => item.site_name === 'Playwright Friend')).toBeTruthy()

  await gotoPage(page, '/')
  await waitForHomeInteractive(page)
  await waitForSubscriptionPopupReady(page)
  await page.locator('[data-subscription-popup-open]').click()
  await expect(page.locator('[data-subscription-popup-panel]')).toBeVisible()

  const popupStatus = page.locator('[data-subscription-popup-status]')
  await expect(popupStatus).toBeVisible()

  const subscribeResponse = await request.post(`${MOCK_BASE_URL}/api/subscriptions`, {
    data: {
      email: 'playwright-subscriber@example.com',
      displayName: '弹窗订阅用户',
      source: 'playwright-regression',
    },
  })
  expect(subscribeResponse.ok()).toBeTruthy()

  const subscriptionState = await getDebugState(request)
  const latest = subscriptionState.subscriptions.find(
    (item: { target: string; display_name: string }) => item.target === 'playwright-subscriber@example.com',
  )
  expect(latest).toBeTruthy()
  expect(latest.display_name).toBe('弹窗订阅用户')

  await gotoPage(page, `/subscriptions/confirm?token=${encodeURIComponent(latest.confirm_token)}`)
  await expect(page.getByText('订阅已确认')).toBeVisible()

  await gotoPage(page, `/subscriptions/manage?token=${encodeURIComponent(latest.manage_token)}`)
  await page.getByRole('textbox', { name: '称呼' }).fill('回归通知')
  await page.getByRole('button', { name: '保存偏好' }).click()
  await expect(page.locator('[data-manage-status]')).toContainText('偏好已保存')

  await gotoPage(page, `/subscriptions/unsubscribe?token=${encodeURIComponent(latest.manage_token)}`)
  await page.getByRole('button', { name: '确认退订' }).click()
  await expect(page.locator('[data-unsubscribe-status]')).toContainText('成功退订')
})

test('维护模式开启后需要口令才能进入前台', async ({ page, request }) => {
  await patchAdminSiteSettings(request, {
    maintenanceModeEnabled: true,
    maintenanceAccessCode: 'staging-2026',
  })

  await gotoPage(page, '/articles/astro-terminal-blog')
  await expect(page).toHaveURL(/\/maintenance\?returnTo=/)
  await expect(page.getByRole('heading', { name: /正在维护/ })).toBeVisible()

  await page.locator('input[name="code"]').fill('wrong-code')
  await page.getByRole('button', { name: '进入站点' }).click()
  await expect(page).toHaveURL(/error=invalid/)
  await expect(page.getByText('口令不正确，请重新输入。')).toBeVisible()

  await page.locator('input[name="code"]').fill('staging-2026')
  await page.getByRole('button', { name: '进入站点' }).click()
  await expect(page).toHaveURL(/\/articles\/astro-terminal-blog$/)
  await expect(page.getByRole('heading', { name: 'Astro 终端博客信息架构实战' })).toBeVisible()

  await gotoPage(page, '/')
  await expect(page).toHaveURL(/\/$/)
  await expect(page.locator('#home-results-count')).toContainText(/条结果/)
})

test('分享面板与 llms 入口可用', async ({ page, request }) => {
  test.setTimeout(120_000)

  await patchAdminSiteSettings(request, {
    seoWechatShareQrEnabled: true,
  })

  await gotoPage(page, '/')
  await waitForHomeInteractive(page)
  await waitForSubscriptionPopupReady(page)
  await expect(page.locator('head link[rel="alternate"][href$="/llms.txt"]')).toHaveCount(1)
  await expect(page.locator('head link[rel="alternate"][href$="/llms-full.txt"]')).toHaveCount(1)
  await expect(page.locator('[data-share-wechat-open]').first()).toBeVisible()

  await gotoPage(page, '/about')
  await expect(page.getByRole('heading', { name: '关于我' })).toBeVisible()
  await expect(page.getByRole('heading', { name: '分享个人介绍' })).toBeVisible()

  await gotoPage(page, '/articles')
  await expect(page).toHaveURL(/\/articles$/)

  await gotoPage(page, '/reviews')
  await expect(page).toHaveURL(/\/reviews$/)

  await gotoPage(page, '/ask')
  await expect(page.getByRole('heading', { name: 'AI 站内问答' })).toBeVisible()

  await gotoPage(page, '/friends')
  await expect(page).toHaveURL(/\/friends$/)

  await gotoPage(page, '/articles/playwright-regression-workflow')
  await page.locator('[data-article-wechat-qr-open]').first().click()
  await expect(page.locator('[data-article-wechat-qr-modal]')).toHaveAttribute('aria-hidden', 'false')
  await expect(page.getByRole('heading', { name: '微信扫一扫' })).toBeVisible()
  await expect(page.locator('[data-article-qr-download]')).toBeVisible()

  await page.locator('[data-article-wechat-qr-close]').first().click()
  await expect(page.locator('[data-article-wechat-qr-modal]')).toHaveAttribute('aria-hidden', 'true')

  await gotoPage(page, '/categories/frontend-engineering')
  await expect(page.getByRole('button', { name: '复制简介' })).toBeVisible()

  await gotoPage(page, '/tags/playwright')
  await expect(page.getByRole('button', { name: '直接分享' })).toBeVisible()

  await gotoPage(page, '/reviews/1')
  await expect(page.getByRole('button', { name: '复制简介' })).toBeVisible()
})
