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

/**
 * 学校端排课功能实际测试
 * 使用真实登录流程和等待策略
 */

const BASE_URL = 'http://localhost:5174';

test.describe('学校端排课功能 - 真实流程测试', () => {
  test.beforeEach(async ({ page }) => {
    // 每个测试前先登录
    await page.goto(`${BASE_URL}/login`);
    await page.waitForTimeout(2000);

    // 先选择学校角色
    await page.click('.role-btn:has-text("学校")');
    await page.waitForTimeout(500);

    // 填写登录表单
    await page.fill('input[placeholder*="账号"]', 'school1');
    await page.fill('input[placeholder*="密码"]', '123456');

    // 点击登录按钮
    await page.click('button:has-text("登录")');

    // 等待登录成功
    await page.waitForURL(/\/school/, { timeout: 15000 });
    await page.waitForTimeout(2000);
  });

  test('应该能访问排课页面', async ({ page }) => {
    // 通过侧边栏导航到排课页面
    const scheduleLink = page.locator('a:has-text("课程排期"), .menu-item:has-text("课程排期")').first();

    const count = await scheduleLink.count();
    if (count > 0) {
      await scheduleLink.click();
      await page.waitForTimeout(2000);
    } else {
      // 直接访问URL
      await page.goto(`${BASE_URL}/school/schedule`);
      await page.waitForLoadState('networkidle', { timeout: 10000 });
    }

    // 截图
    await page.screenshot({ path: 'screenshots/schedule-page.png' });

    // 检查页面标题
    const title = await page.locator('h1, h2, .page-header').first().textContent();
    console.log('✅ 页面标题:', title);
  });

  test('应该能查看列表视图', async ({ page }) => {
    await page.goto(`${BASE_URL}/school/schedule`);
    await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
    await page.waitForTimeout(3000);

    // 截图
    await page.screenshot({ path: 'screenshots/schedule-list-view.png' });

    // 检查是否有Tab
    const tabs = page.locator('.ant-tabs-tab, [role="tab"]');
    const tabCount = await tabs.count();
    console.log(`✅ 找到 ${tabCount} 个Tab`);

    // 检查是否有表格
    const table = page.locator('.ant-table');
    const hasTable = await table.count() > 0;
    console.log(`✅ 有表格: ${hasTable}`);
  });

  test('应该能测试后端API', async ({ page }) => {
    // 先登录获取token
    await page.goto(`${BASE_URL}/login`);
    await page.fill('input[placeholder*="账号"], input[name="username"]', 'school1');
    await page.fill('input[placeholder*="密码"], input[name="password"]', '123456');
    await page.click('button[type="submit"]');
    await page.waitForURL(/\/school/, { timeout: 10000 });

    // 从localStorage获取token
    const token = await page.evaluate(() => localStorage.getItem('token'));

    // 测试课程套餐API
    const packagesResponse = await page.request.get(`${BASE_URL}/api/v1/school/packages`, {
      headers: { 'Authorization': `Bearer ${token}` }
    });

    console.log('✅ 课程套餐API状态:', packagesResponse.status());
    expect(packagesResponse.ok()).toBeTruthy();

    const packagesData = await packagesResponse.json();
    console.log('✅ 课程套餐数据:', JSON.stringify(packagesData).substring(0, 200));

    // 测试排课列表API
    const schedulesResponse = await page.request.get(`${BASE_URL}/api/v1/school/schedules?pageNum=1&pageSize=10`, {
      headers: { 'Authorization': `Bearer ${token}` }
    });

    console.log('✅ 排课列表API状态:', schedulesResponse.status());
    expect(schedulesResponse.ok()).toBeTruthy();
  });
});
