import { test as base, type Browser, type Page } from '@playwright/test';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { AdminLoginPage } from '../pages/admin/login.page';
import { DashboardPage } from '../pages/admin/dashboard.page';
import { UserManagementPage } from '../pages/admin/user-management.page';
import { DisabilityPersonManagementPage } from '../pages/admin/disability-person.page';
import { RegionManagementPage } from '../pages/admin/region-management.page';
import { OrderManagementPage } from '../pages/admin/order-management.page';
import { PlatformManagementPage } from '../pages/admin/platform-management.page';
import { CompanyManagementPage } from '../pages/admin/company-management.page';
import { ChannelManagementPage } from '../pages/admin/channel-management.page';
import { EnterpriseMiniPage } from '../pages/mini/enterprise-mini.page';
import { TalentMiniPage } from '../pages/mini/talent-mini.page';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const testUsers = JSON.parse(readFileSync(join(__dirname, '../fixtures/test-users.json'), 'utf-8'));

type Fixtures = {
  browser: Browser;
  // 跨端测试专用 fixtures（使用独立的 browser context）
  adminPage: Page;
  talentMiniPage: Page;
  // 原有的 Page Object fixtures
  adminLoginPage: AdminLoginPage;
  dashboardPage: DashboardPage;
  userManagementPage: UserManagementPage;
  disabilityPersonPage: DisabilityPersonManagementPage;
  regionManagementPage: RegionManagementPage;
  orderManagementPage: OrderManagementPage;
  platformManagementPage: PlatformManagementPage;
  companyManagementPage: CompanyManagementPage;
  channelManagementPage: ChannelManagementPage;
  enterpriseMiniPage: EnterpriseMiniPage;
  talentMiniPageObj: TalentMiniPage;
  testUsers: typeof testUsers;
};

export const test = base.extend<Fixtures>({
  browser: async ({ browser }, use) => {
    await use(browser);
  },
  // 跨端测试专用 fixtures - 使用独立的 browser context
  adminPage: async ({ browser }, use) => {
    const context = await browser.newContext();
    const page = await context.newPage();
    await use(page);
    await context.close();
  },
  talentMiniPage: async ({ browser }, use) => {
    const context = await browser.newContext();
    const page = await context.newPage();
    await use(page);
    await context.close();
  },
  // 原有的 Page Object fixtures（保持兼容性）
  adminLoginPage: async ({ page }, use) => {
    await use(new AdminLoginPage(page));
  },
  dashboardPage: async ({ page }, use) => {
    await use(new DashboardPage(page));
  },
  userManagementPage: async ({ page }, use) => {
    await use(new UserManagementPage(page));
  },
  disabilityPersonPage: async ({ page }, use) => {
    await use(new DisabilityPersonManagementPage(page));
  },
  regionManagementPage: async ({ page }, use) => {
    await use(new RegionManagementPage(page));
  },
  orderManagementPage: async ({ page }, use) => {
    await use(new OrderManagementPage(page));
  },
  platformManagementPage: async ({ page }, use) => {
    await use(new PlatformManagementPage(page));
  },
  companyManagementPage: async ({ page }, use) => {
    await use(new CompanyManagementPage(page));
  },
  channelManagementPage: async ({ page }, use) => {
    await use(new ChannelManagementPage(page));
  },
  enterpriseMiniPage: async ({ page }, use) => {
    await use(new EnterpriseMiniPage(page));
  },
  // 重命名为 talentMiniPageObj 以避免与新的 talentMiniPage fixture 冲突
  talentMiniPageObj: async ({ page }, use) => {
    await use(new TalentMiniPage(page));
  },
  // 自定义 fixture 不需要依赖其他 fixtures 时，使用空对象解构参数
  // eslint-disable-next-line no-empty-pattern
  testUsers: async ({}, use) => {
    await use(testUsers);
  },
});

// 在所有测试前设置测试模式标志
test.beforeEach(async ({ page }) => {
  await page.addInitScript(() => {
    (window as Window & { __PLAYWRIGHT_TEST__?: boolean }).__PLAYWRIGHT_TEST__ = true;
  });
});

export { expect } from '@playwright/test';
