import { test as base, type Browser } 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';

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;
  adminLoginPage: AdminLoginPage;
  dashboardPage: DashboardPage;
  userManagementPage: UserManagementPage;
  disabilityPersonPage: DisabilityPersonManagementPage;
  regionManagementPage: RegionManagementPage;
  orderManagementPage: OrderManagementPage;
  platformManagementPage: PlatformManagementPage;
  companyManagementPage: CompanyManagementPage;
  channelManagementPage: ChannelManagementPage;
  enterpriseMiniPage: EnterpriseMiniPage;
  testUsers: typeof testUsers;
};

export const test = base.extend<Fixtures>({
  browser: async ({ browser }, use) => {
    await use(browser);
  },
  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));
  },
  testUsers: async (_fixtures: unknown, 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';
