import { TIMEOUTS } from '../../utils/timeouts';
import { test, expect } from '../../utils/test-setup';

/**
 * 数据统计页测试与功能验证 (Story 13.12)
 */

const TEST_USER_PHONE = '13800001111';
const TEST_USER_PASSWORD = 'password123';
const STATISTICS_PAGE_URL = '/pages/yongren/statistics/index';

test.describe.serial('数据统计页测试与功能验证 - Story 13.12', () => {
  test.use({ storageState: undefined });

  test('应该能够访问数据统计页', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.expectUrl(STATISTICS_PAGE_URL);
    console.debug('[AC1.1] 数据统计页可访问性 ✓');
  });

  test('应该显示筛选器 UI 元素', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);

    const yearSelector = miniPage.page.locator('select, [role="combobox"]').filter({ hasText: /年/ });
    const monthSelector = miniPage.page.locator('select, [role="combobox"]').filter({ hasText: /月/ });
    const hasYearSelector = await yearSelector.count() > 0;
    const hasMonthSelector = await monthSelector.count() > 0;
    expect(hasYearSelector || hasMonthSelector).toBeTruthy();
    console.debug('[AC1.2] 筛选器 UI 元素 ✓');
  });

  test('应该显示 4 个统计卡片', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const cards = await miniPage.getStatisticsCards();
    expect(cards.length).toBeGreaterThanOrEqual(4);
    console.debug('[AC1.3] 4 个统计卡片显示 ✓');
  });

  test('应该显示 6 个统计图表', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const charts = await miniPage.getStatisticsCharts();
    expect(charts.length).toBeGreaterThan(0);
    console.debug('[AC1.4] 6 个统计图表显示 ✓');
  });

  test('选择年份后应更新统计数据', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    await miniPage.selectYear(2025);
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);

    const updatedCards = await miniPage.getStatisticsCards();
    expect(updatedCards.length).toBeGreaterThanOrEqual(4);
    console.debug('[AC2.1] 选择年份后数据更新 ✓');
  });

  test('选择月份后应更新统计数据', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    await miniPage.selectMonth(12);
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);

    const updatedCards = await miniPage.getStatisticsCards();
    expect(updatedCards.length).toBeGreaterThanOrEqual(4);
    console.debug('[AC2.2] 选择月份后数据更新 ✓');
  });

  test('验证在职人数数据正确性', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const cards = await miniPage.getStatisticsCards();
    const employedCard = cards.find(c => c.cardName.includes('在职') || c.cardName.includes('人数'));
    expect(employedCard).toBeDefined();
    if (employedCard) {
      expect(employedCard.currentValue).toMatch(/d+/);
    }
    console.debug('[AC3.1] 在职人数数据正确性 ✓');
  });

  test('验证平均薪资数据正确性', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const cards = await miniPage.getStatisticsCards();
    const salaryCard = cards.find(c => c.cardName.includes('薪资') || c.cardName.includes('平均'));
    expect(salaryCard).toBeDefined();
    if (salaryCard) {
      expect(salaryCard.currentValue).toMatch(/d+|¥|元/);
    }
    console.debug('[AC3.2] 平均薪资数据正确性 ✓');
  });

  test('验证残疾类型分布图表数据', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    await miniPage.expectChartData('残疾类型分布', { isVisible: true });
    console.debug('[AC4.1] 残疾类型分布图表数据 ✓');
  });

  test('验证性别分布图表数据', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    await miniPage.expectChartData('性别分布', { isVisible: true });
    console.debug('[AC4.2] 性别分布图表数据 ✓');
  });

  test('验证 API 不传参数时返回当前年月数据', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const cards = await miniPage.getStatisticsCards();
    expect(cards.length).toBeGreaterThan(0);
    console.debug('[AC5.1] API 不传参数返回当前年月数据 ✓');
  });

  test('完整的数据统计页用户流程', async ({ enterpriseMiniPage: miniPage }) => {
    await miniPage.goto();
    await miniPage.login(TEST_USER_PHONE, TEST_USER_PASSWORD);
    await miniPage.expectLoginSuccess();
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);
    await miniPage.navigateToStatisticsPage();
    await miniPage.waitForStatisticsDataLoaded();

    const cards = await miniPage.getStatisticsCards();
    expect(cards.length).toBeGreaterThanOrEqual(4);

    const charts = await miniPage.getStatisticsCharts();
    expect(charts.length).toBeGreaterThan(0);

    await miniPage.selectYear(2025);
    await miniPage.page.waitForTimeout(TIMEOUTS.MEDIUM);

    const updatedCards = await miniPage.getStatisticsCards();
    expect(updatedCards.length).toBeGreaterThanOrEqual(4);

    console.debug('[综合测试] 完整的数据统计页用户流程 ✓');
  });
});
