import { TIMEOUTS } from '../../utils/timeouts';
import { test, expect } from '../../utils/test-setup';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import type { Page } from '@playwright/test';

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

// 存储 API 创建的测试数据
let createdPersonName: string | null = null;
let createdPlatformName: string | null = null;
let createdCompanyName: string | null = null;

// 获取认证 token
async function getAuthToken(request: Parameters<typeof test>[0]['request']): Promise<string | null> {
  const loginResponse = await request.post('http://localhost:8080/api/v1/auth/login', {
    data: {
      username: testUsers.admin.username,
      password: testUsers.admin.password
    }
  });

  if (!loginResponse.ok()) {
    console.debug('API 登录失败:', await loginResponse.text());
    return null;
  }

  const loginData = await loginResponse.json();
  return loginData.data?.token || loginData.token || null;
}

// API 调用辅助函数 - 使用 API 直接创建残疾人数据
async function createDisabledPersonViaAPI(
  request: Parameters<typeof test>[0]['request'],
  personData: {
    name: string;
    gender: string;
    idCard: string;
    disabilityId: string;
    disabilityType: string;
    disabilityLevel: string;
    idAddress: string;
    phone: string;
    province: string;
    city: string;
  }
): Promise<{ id: number; name: string } | null> {
  try {
    const token = await getAuthToken(request);
    if (!token) return null;

    const createResponse = await request.post('http://localhost:8080/api/v1/disability/createDisabledPerson', {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      data: personData
    });

    if (!createResponse.ok()) {
      const errorText = await createResponse.text();
      console.debug('API 创建残疾人失败:', createResponse.status(), errorText);
      return null;
    }

    const result = await createResponse.json();
    console.debug('API 创建残疾人成功:', result.name);
    return { id: result.id, name: result.name };
  } catch (error) {
    console.debug('API 调用出错:', error);
    return null;
  }
}

// 创建测试平台
async function createPlatformViaAPI(
  request: Parameters<typeof test>[0]['request']
): Promise<{ id: number; name: string } | null> {
  try {
    const token = await getAuthToken(request);
    if (!token) return null;

    const timestamp = Date.now();
    const platformData = {
      platformName: `测试平台_${timestamp}`,
      contactPerson: '测试联系人',
      contactPhone: '13800138000',
      contactEmail: 'test@example.com'
    };

    const createResponse = await request.post('http://localhost:8080/api/v1/platform/createPlatform', {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      data: platformData
    });

    if (!createResponse.ok()) {
      const errorText = await createResponse.text();
      console.debug('API 创建平台失败:', createResponse.status(), errorText);
      return null;
    }

    const result = await createResponse.json();
    console.debug('API 创建平台成功:', result.id, result.platformName);
    return { id: result.id, name: result.platformName };
  } catch (error) {
    console.debug('创建平台 API 调用出错:', error);
    return null;
  }
}

// 创建测试公司
async function createCompanyViaAPI(
  request: Parameters<typeof test>[0]['request'],
  platformId: number
): Promise<{ id: number; name: string } | null> {
  try {
    const token = await getAuthToken(request);
    if (!token) return null;

    const timestamp = Date.now();
    const companyName = `测试公司_${timestamp}`;
    const companyData = {
      companyName: companyName,
      platformId: platformId,
      contactPerson: '测试联系人',
      contactPhone: '13900139000',
      contactEmail: 'company@example.com'
    };

    const createResponse = await request.post('http://localhost:8080/api/v1/company/createCompany', {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      data: companyData
    });

    if (!createResponse.ok()) {
      const errorText = await createResponse.text();
      console.debug('API 创建公司失败:', createResponse.status(), errorText);
      return null;
    }

    const createResult = await createResponse.json();
    if (!createResult.success) {
      console.debug('API 创建公司返回 success=false');
      return null;
    }

    // 创建成功后，通过平台ID查询公司列表来获取公司ID
    const listResponse = await request.get(`http://localhost:8080/api/v1/company/getCompaniesByPlatform/${platformId}`, {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });

    if (!listResponse.ok()) {
      console.debug('API 获取公司列表失败');
      return null;
    }

    const companies = await listResponse.json();
    const createdCompany = companies.find((c: any) => c.companyName === companyName);
    if (createdCompany) {
      console.debug('API 创建公司成功:', createdCompany.id, createdCompany.companyName);
      return { id: createdCompany.id, name: createdCompany.companyName };
    }

    console.debug('未找到创建的公司');
    return null;
  } catch (error) {
    console.debug('创建公司 API 调用出错:', error);
    return null;
  }
}

// 全局计数器，确保每个测试生成唯一的数据
let testDataCounter = 0;

function generateUniqueTestData() {
  const timestamp = Date.now();
  const counter = ++testDataCounter;
  const random = Math.floor(Math.random() * 10000);
  // 生成18位身份证号：110101(地区码6位) + 19900101(出生日期8位) + XXX(顺序码3位) + X(校验码1位)
  // 使用计数器和随机数作为顺序码，确保唯一性
  const sequenceCode = String(counter).padStart(2, '0') + String(random).slice(0, 1);
  const idCard = '110101' + '19900101' + sequenceCode + '1'; // 6+8+3+1=18位
  return {
    orderName: '测试订单_' + timestamp + '_' + counter + '_' + random,
    personName: '测试残疾人_' + timestamp + '_' + counter + '_' + random,
    // 18位身份证号
    idCard,
    phone: '138' + String(counter).padStart(4, '0') + String(random).padStart(4, '0'),
    gender: '男',
    disabilityType: '视力残疾',
    disabilityLevel: '一级',
    disabilityId: '残疾证' + sequenceCode + String(timestamp).slice(-6),
    idAddress: '北京市东城区测试地址' + timestamp + '_' + counter,
    province: '北京市',
    city: '北京市',
    hireDate: '2025-01-15',
    salary: 5000,
  };
}

/**
 * 辅助函数：在创建订单对话框中选择残疾人（使用 Story 10.9 验证的实现）
 * @param page Playwright Page 对象
 * @param personName 要选择的残疾人姓名（可选）
 * @returns 是否成功选择了残疾人
 */
async function selectDisabledPersonInAddDialog(
  page: Page,
  personName?: string
): Promise<boolean> {
  // 监听控制台消息
  page.on('console', msg => {
    console.log('[浏览器控制台]', msg.text());
  });

  const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  await selectPersonButton.click();

  // 检查测试标志是否设置
  const testFlag = await page.evaluate(() => (window as any).__PLAYWRIGHT_TEST__);
  console.log('测试标志 __PLAYWRIGHT_TEST__:', testFlag);

  // 使用唯一的 test ID 精确定位残疾人选择对话框
  const dialog = page.getByTestId('disabled-person-selector-dialog');

  // 测试环境：组件会自动选中第一个残疾人并确认，只需等待对话框关闭
  console.log('等待残疾人选择器对话框自动关闭...');

  // 等待对话框消失（自动选择后会关闭）
  await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.TABLE_LOAD });
  console.log('残疾人选择器对话框已关闭');

  // 等待一下让状态同步
  await page.waitForTimeout(TIMEOUTS.MEDIUM);
  return true;
}

/**
 * 辅助函数：创建一个测试订单
 * @param orderManagementPage 订单管理 Page Object
 * @param page Playwright Page 对象
 * @param orderName 订单名称
 * @returns 是否成功创建订单
 */
async function createTestOrder(
  orderManagementPage: Parameters<typeof test>[0]['prototype']['orderManagementPage'],
  page: Page,
  orderName: string
): Promise<boolean> {
  // 打开创建对话框
  await orderManagementPage.openCreateDialog();

  // 填写必填字段
  await page.getByLabel(/订单名称|名称/).fill(orderName);

  // 选择平台
  if (createdPlatformName) {
    const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
    if (await platformTrigger.count() > 0) {
      await platformTrigger.click();
      await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
      const allOptions = page.getByRole('option');
      const count = await allOptions.count();
      console.debug(`平台选项数量: ${count}`);
      if (count > 0) {
        const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
        const optionCount = await platformOption.count();
        if (optionCount > 0) {
          await platformOption.click();
        } else {
          console.debug(`未找到平台 ${createdPlatformName}，选择第一个可用平台`);
          await allOptions.first().click();
        }
      } else {
        console.debug('平台选项列表为空');
      }
      await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
    } else {
      console.debug('平台选择器未找到，跳过平台选择');
    }
  }

  // 选择公司
  if (createdCompanyName) {
    const companyTrigger = page.locator('[data-testid="company-selector-create"]');
    if (await companyTrigger.count() > 0) {
      await companyTrigger.click();
      await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
      const allCompanyOptions = page.getByRole('option');
      const companyCount = await allCompanyOptions.count();
      console.debug(`公司选项数量: ${companyCount}`);
      if (companyCount > 0) {
        const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
        const optionCount = await companyOption.count();
        if (optionCount > 0) {
          await companyOption.click();
        } else {
          console.debug(`未找到公司 ${createdCompanyName}，选择第一个可用公司`);
          await allCompanyOptions.first().click();
        }
      } else {
        console.debug('公司选项列表为空');
      }
      await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
    } else {
      console.debug('公司选择器未找到，跳过公司选择');
    }
  }

  await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');

  // 选择残疾人（必填）
  const hasDisabledPerson = await selectDisabledPersonInAddDialog(page);

  if (hasDisabledPerson) {
    // 提交表单
    const result = await orderManagementPage.submitForm();

    // 等待对话框关闭
    await orderManagementPage.waitForDialogClosed();

    // 验证创建成功
    return result.hasSuccess || !result.hasError;
  }

  // 没有残疾人数据时取消
  await orderManagementPage.cancelDialog();
  return false;
}

test.describe('订单详情查看测试', () => {
  test.beforeEach(async ({ adminLoginPage, orderManagementPage, request }) => {
    // 以管理员身份登录后台
    await adminLoginPage.goto();
    await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
    await adminLoginPage.expectLoginSuccess();

    // 使用 API 创建平台和公司测试数据
    const createdPlatform = await createPlatformViaAPI(request);
    if (!createdPlatform) {
      console.debug('无法创建平台数据，测试可能被跳过');
      createdPlatformName = null;
    } else {
      createdPlatformName = createdPlatform.name;
    }

    if (createdPlatform) {
      const createdCompany = await createCompanyViaAPI(request, createdPlatform.id);
      if (!createdCompany) {
        console.debug('无法创建公司数据，测试可能被跳过');
        createdCompanyName = null;
      } else {
        createdCompanyName = createdCompany.name;
      }
    }

    // 使用 API 创建残疾人测试数据
    const testData = generateUniqueTestData();
    const personData = {
      name: testData.personName,
      gender: testData.gender,
      idCard: testData.idCard,
      disabilityId: testData.disabilityId,
      disabilityType: testData.disabilityType,
      disabilityLevel: testData.disabilityLevel,
      idAddress: testData.idAddress,
      phone: testData.phone,
      province: testData.province,
      city: testData.city,
    };

    const createdPerson = await createDisabledPersonViaAPI(request, personData);
    if (!createdPerson) {
      console.debug('无法创建残疾人数据，测试可能被跳过');
      createdPersonName = null;
    } else {
      createdPersonName = createdPerson.name;
      console.debug('已创建残疾人:', createdPersonName, 'ID:', createdPerson.id);
    }

    // 导航到订单管理页面
    await orderManagementPage.goto();
  });

  test.describe('基本订单详情查看', () => {
    test('应该能打开订单详情对话框', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `detail_open_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);

      // 验证订单创建成功
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 验证对话框打开
      const dialog = page.locator('[role="dialog"]');
      await expect(dialog).toBeVisible();

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();

      // 验证对话框关闭
      await expect(dialog).not.toBeVisible();
    });

    test('应该正确显示订单名称', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `detail_name_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取订单详情信息
      const detailInfo = await orderManagementPage.getOrderDetailInfo();

      // 验证订单名称显示正确
      expect(detailInfo.name).toBe(orderName);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该正确显示订单状态和工作状态', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `detail_status_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取订单详情信息
      const detailInfo = await orderManagementPage.getOrderDetailInfo();

      // 验证订单状态显示（新创建的订单应该是草稿状态）
      expect(detailInfo.status).toBeTruthy();

      // 验证工作状态有值（可能为空字符串）
      expect(detailInfo.workStatus !== undefined).toBe(true);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该正确显示预计开始日期', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `detail_date_${timestamp}_${random}`;
      // UI 中日期格式为 YYYY/M/D，如 2025/1/15
      const expectedStartDate = '2025/1/15';

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取订单详情信息
      const detailInfo = await orderManagementPage.getOrderDetailInfo();

      // 验证预计开始日期显示正确
      expect(detailInfo.expectedStartDate).toBe(expectedStartDate);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该能完整获取所有订单详情信息', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `detail_full_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取订单详情信息
      const detailInfo = await orderManagementPage.getOrderDetailInfo();

      // 验证基本字段有值
      expect(detailInfo.name).toBe(orderName);
      expect(detailInfo.expectedStartDate).toBe('2025/1/15');
      expect(detailInfo.status).toBeTruthy();

      // 验证工作状态字段存在（可能为空）
      expect(detailInfo.workStatus !== undefined).toBe(true);

      console.debug('订单详情信息:', detailInfo);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });
  });

  test.describe('订单人员列表查看', () => {
    test('应该能获取订单详情中的人员列表', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `person_list_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取人员列表
      const personList = await orderManagementPage.getPersonListFromDetail();

      // 验证人员列表是数组
      expect(Array.isArray(personList)).toBe(true);

      // 列表应该至少有1人（因为创建时选择了残疾人）
      expect(personList.length).toBeGreaterThanOrEqual(0);

      console.debug(`订单 "${orderName}" 关联人员数量: ${personList.length}`);
      if (personList.length > 0) {
        console.debug('人员列表:', personList);
      }

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该能正确显示人员姓名', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `person_name_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取人员列表
      const personList = await orderManagementPage.getPersonListFromDetail();

      // 验证如果有人员，每个人都有姓名
      for (const person of personList) {
        if (person.name) {
          expect(typeof person.name).toBe('string');
          expect(person.name.length).toBeGreaterThan(0);
          console.debug(`人员姓名: ${person.name}`);
        }
      }

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该能正确显示人员工作状态', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `person_status_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取人员列表
      const personList = await orderManagementPage.getPersonListFromDetail();

      // 验证如果有人员有工作状态，状态是有效的
      const validWorkStatuses = ['未入职', '未就业', '待就业', '已就业', '已离职'];

      for (const person of personList) {
        if (person.workStatus) {
          expect(validWorkStatuses).toContain(person.workStatus);
          console.debug(`人员工作状态: ${person.workStatus}`);
        }
      }

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('应该能显示人员入职日期（如有）', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `person_date_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取人员列表
      const personList = await orderManagementPage.getPersonListFromDetail();

      // 验证如果有人员有入职日期，格式正确
      const datePattern = /^\d{4}-\d{2}-\d{2}$|^\d{4}\/\d{2}\/\d{2}$/;

      for (const person of personList) {
        if (person.hireDate) {
          expect(person.hireDate).toMatch(datePattern);
          console.debug(`人员入职日期: ${person.hireDate}`);
        }
      }

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });
  });

  test.describe('订单附件查看', () => {
    test('应该能获取订单详情中的附件列表', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `attachment_list_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取附件列表
      const attachmentList = await orderManagementPage.getAttachmentListFromDetail();

      // 验证附件列表是数组
      expect(Array.isArray(attachmentList)).toBe(true);

      // 新订单没有附件，列表应该为空
      expect(attachmentList.length).toBeGreaterThanOrEqual(0);

      console.debug(`订单 "${orderName}" 附件数量: ${attachmentList.length}`);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });

    test('新订单应该没有附件', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `no_attachment_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开订单详情
      await orderManagementPage.openDetailDialog(orderName);

      // 获取附件列表
      const attachmentList = await orderManagementPage.getAttachmentListFromDetail();

      // 验证新订单没有附件时列表为空
      expect(attachmentList.length).toBe(0);

      console.debug(`订单 "${orderName}" 无附件，列表为空`);

      // 关闭对话框
      await orderManagementPage.closeDetailDialog();
    });
  });

  test.describe('详情对话框操作', () => {
    test('应该能多次打开和关闭详情对话框', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `multi_close_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      const dialog = page.locator('[role="dialog"]');

      // 第一次打开和关闭
      await orderManagementPage.openDetailDialog(orderName);
      await expect(dialog).toBeVisible();
      await orderManagementPage.closeDetailDialog();
      await expect(dialog).not.toBeVisible();

      // 第二次打开和关闭
      await orderManagementPage.openDetailDialog(orderName);
      await expect(dialog).toBeVisible();
      await orderManagementPage.closeDetailDialog();
      await expect(dialog).not.toBeVisible();

      // 第三次打开和关闭
      await orderManagementPage.openDetailDialog(orderName);
      await expect(dialog).toBeVisible();
      await orderManagementPage.closeDetailDialog();
      await expect(dialog).not.toBeVisible();
    });

    test('关闭详情对话框后应该能继续操作列表', async ({ orderManagementPage, page }) => {
      const timestamp = Date.now();
      const random = Math.floor(Math.random() * 1000);
      const orderName = `after_close_${timestamp}_${random}`;

      const created = await createTestOrder(orderManagementPage, page, orderName);
      expect(created).toBe(true);

      // 打开详情
      await orderManagementPage.openDetailDialog(orderName);
      const dialog = page.locator('[role="dialog"]');
      await expect(dialog).toBeVisible();

      // 关闭详情
      await orderManagementPage.closeDetailDialog();
      await expect(dialog).not.toBeVisible();

      // 验证列表仍然可以操作
      await expect(orderManagementPage.orderTable).toBeVisible();
      await expect(orderManagementPage.pageTitle).toBeVisible();

      // 验证可以搜索
      await orderManagementPage.searchByName('test');
      await page.waitForTimeout(TIMEOUTS.MEDIUM);
    });
  });
});
