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

test.describe('首页 (/)', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/');
  });

  test('页面标题正确', async ({ page }) => {
    await expect(page).toHaveTitle(/宇之然 AI/);
  });

  test('Hero 区域渲染', async ({ page }) => {
    await expect(page.getByText('免费 AI 知识社区')).toBeVisible();
    await expect(page.getByText('让每个人').first()).toBeVisible();
    await expect(page.getByText('都能用好 AI').first()).toBeVisible();
    await expect(page.getByText('宇之然 AI 是面向大众的 AI 工具与知识社区')).toBeVisible();
  });

  test('Hero 按钮存在并指向正确链接', async ({ page }) => {
    const startBtn = page.getByText('开始探索').first();
    await expect(startBtn).toBeVisible();
    await expect(startBtn).toHaveAttribute('href', '/courses');

    const registerBtn = page.getByText('免费注册').first();
    await expect(registerBtn).toBeVisible();
    await expect(registerBtn).toHaveAttribute('href', '/auth?tab=register');
  });

  test('统计数据区域', async ({ page }) => {
    await expect(page.getByText('50+').first()).toBeVisible();
    await expect(page.getByText('200+').first()).toBeVisible();
    await expect(page.getByText('30+').first()).toBeVisible();
    await expect(page.getByText('10,000+').first()).toBeVisible();
  });

  test('特性区域', async ({ page }) => {
    await expect(page.getByText('为什么选择宇之然？')).toBeVisible();
    await expect(page.getByText('分领域指南').first()).toBeVisible();
    await expect(page.getByText('AI 沙盒实战').first()).toBeVisible();
    await expect(page.getByText('提示词库').first()).toBeVisible();
    await expect(page.getByText('持续更新').first()).toBeVisible();
  });

  test('热门专题区域', async ({ page }) => {
    await expect(page.getByText('热门专题')).toBeVisible();
    await expect(page.getByText('AI 通识：零基础入门')).toBeVisible();
    await expect(page.getByText('提示词工程从入门到精通')).toBeVisible();
    await expect(page.getByText('用 AI 提升 10 倍办公效率')).toBeVisible();
  });

  test('AI 沙盒预览区域', async ({ page }) => {
    await expect(page.getByText('AI 沙盒').first()).toBeVisible();
    await expect(page.getByText('在线体验 AI 对话，边学边练')).toBeVisible();
    const sandboxLink = page.getByText('打开沙盒').first();
    await expect(sandboxLink).toHaveAttribute('href', '/sandbox');
  });

  test('CTA 区域', async ({ page }) => {
    await expect(page.getByText('准备好开启 AI 之旅了吗？')).toBeVisible();
    await expect(page.getByText('立即注册，免费探索所有内容')).toBeVisible();
  });

  test('点击 Hero 开始探索进入课程页', async ({ page }) => {
    await page.getByText('开始探索').first().click();
    await expect(page).toHaveURL(/\/courses/);
  });
});
