// @ts-check
const { test, expect } = require('@playwright/test');

/**
 * 充电桩功率别名测试
 * 验证 1、4、14 号桩显示 5kW 而非 7kW
 */

test.describe('充电桩功率别名', () => {

  test.beforeEach(async ({ page }) => {
    await page.goto('/?mock=true');
    await page.waitForSelector('#dc-grid > *', { timeout: 10000 });
  });

  test('1号枪显示 5kW', async ({ page }) => {
    const card = page.locator('#ac-grid > div').filter({ hasText: '1号枪' });
    await expect(card).toBeVisible();
    await expect(card).toContainText('5kW');
    // 确保不显示 7kW
    const text = await card.innerText();
    expect(text).not.toContain('7kW');
  });

  test('4号桩显示 5kW', async ({ page }) => {
    const card = page.locator('#ac-grid > div').filter({ hasText: '4号桩' });
    await expect(card).toBeVisible();
    await expect(card).toContainText('5kW');
    const text = await card.innerText();
    expect(text).not.toContain('7kW');
  });

  test('14号枪显示 5kW', async ({ page }) => {
    const card = page.locator('#ac-grid > div').filter({ hasText: '14号枪' });
    await expect(card).toBeVisible();
    await expect(card).toContainText('5kW');
    const text = await card.innerText();
    expect(text).not.toContain('7kW');
  });

  test('其他桩显示 7kW', async ({ page }) => {
    // 5号桩应该显示 7kW（选择器更精确）
    const card = page.locator('#ac-grid > div').filter({ hasText: '5号桩' });
    await expect(card).toBeVisible();
    await expect(card).toContainText('7kW');
  });

  test('5kW 桩样式与 7kW 不同', async ({ page }) => {
    const card5kw = page.locator('#ac-grid > div').filter({ hasText: '1号枪' });
    const card7kw = page.locator('#ac-grid > div').filter({ hasText: '2号桩' });

    // 检查 5kW 桩是否有特殊样式类
    const class5kw = await card5kw.first().getAttribute('class');
    expect(class5kw).toContain('power-5kw');

    // 检查 7kW 桩没有特殊样式类
    const class7kw = await card7kw.first().getAttribute('class');
    expect(class7kw).not.toContain('power-5kw');
  });
});

test.describe('周末订阅栏显示', () => {

  test.beforeEach(async ({ page }) => {
    await page.goto('/?mock=true');
    await page.waitForSelector('#dc-grid > *', { timeout: 10000 });
  });

  test('周末订阅栏置灰显示', async ({ page }) => {
    const bottomBar = page.locator('#bottom-bar');

    // 周末应该可见，但置灰
    await expect(bottomBar).toBeVisible();

    // 检查是否有禁用样式
    const className = await bottomBar.getAttribute('class');
    const isWeekend = new Date().getDay() === 0 || new Date().getDay() === 6;

    if (isWeekend) {
      expect(className).toContain('opacity-50');
    }
  });

  test('周末订阅栏显示"无法周末订阅"', async ({ page }) => {
    const isWeekend = new Date().getDay() === 0 || new Date().getDay() === 6;

    if (isWeekend) {
      const bottomBar = page.locator('#bottom-bar');
      await expect(bottomBar).toContainText('无法周末订阅');
    } else {
      test.skip();
    }
  });

  test('周末订阅按钮禁用', async ({ page }) => {
    const isWeekend = new Date().getDay() === 0 || new Date().getDay() === 6;

    if (isWeekend) {
      const subscribeBtn = page.locator('#bottom-bar button:has-text("订阅提醒")');
      await expect(subscribeBtn).toBeDisabled();
    } else {
      test.skip();
    }
  });

  test('工作日订阅栏正常显示', async ({ page }) => {
    const isWeekend = new Date().getDay() === 0 || new Date().getDay() === 6;

    if (!isWeekend) {
      const bottomBar = page.locator('#bottom-bar');
      await expect(bottomBar).toBeVisible();

      // 检查没有禁用样式
      const className = await bottomBar.getAttribute('class');
      expect(className).not.toContain('opacity-50');

      // 按钮应该可用
      const subscribeBtn = page.locator('#bottom-bar button:has-text("订阅提醒")');
      await expect(subscribeBtn).toBeEnabled();
    } else {
      test.skip();
    }
  });
});
