/**
 * 订单状态流转 E2E 测试
 *
 * 测试范围：
 * - 激活草稿订单
 * - 关闭进行中订单
 * - 状态限制验证
 * - 状态流转后的 Toast 消息验证
 *
 * @packageDocumentation
 */

import { TIMEOUTS } from '../../utils/timeouts';
import { test, expect, Page } from '../../utils/test-setup';
import { ORDER_STATUS, type OrderStatus, ORDER_STATUS_LABELS } from '../../pages/admin/order-management.page';


/**
 * 辅助函数：在创建订单对话框中选择残疾人
 *
 * @param page - Playwright Page 对象
 * @returns 是否成功选择了残疾人
 */
async function selectDisabledPersonForOrder(page: Page): Promise<boolean> {
  const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  await selectPersonButton.click();

  await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });

  let hasData = false;
  try {
    const firstCheckbox = page.locator('table tbody tr').first().locator('input[type="checkbox"]').first();
    await firstCheckbox.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
    await firstCheckbox.check();
    console.debug('✓ 已选择第一个残疾人');
    hasData = true;
  } catch (error) {
    console.debug('没有可用的残疾人数据');
    hasData = false;
  }

  if (hasData) {
    const confirmButton = page.getByRole('button', { name: /^(确定|确认|选择)$/ });
    await confirmButton.click().catch(() => {
      console.debug('没有找到确认按钮，尝试关闭对话框');
      page.keyboard.press('Escape');
    });
  } else {
    await page.keyboard.press('Escape').catch(() => {
      console.debug('无法关闭对话框，可能已经自动关闭');
    });
  }

  // 等待对话框关闭
  await page.waitForSelector('[role="dialog"]', { state: 'hidden', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT })
    .catch(() => console.debug('对话框已关闭或不存在'));
  return hasData;
}

/**
 * 辅助函数：创建测试订单
 *
 * @param page - Playwright Page 对象
 * @param orderName - 订单名称
 * @param status - 订单状态（默认为草稿）
 * @returns 是否成功创建
 */
async function createTestOrder(page: Page, orderName: string, status?: OrderStatus): Promise<boolean> {
  // 打开创建对话框
  const addOrderButton = page.getByTestId('create-order-button');
  await addOrderButton.click();
  await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });

  // 填写必填字段
  await page.getByLabel(/订单名称|名称/).fill(orderName);
  await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');

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

  if (!hasDisabledPerson) {
    await page.keyboard.press('Escape');
    return false;
  }

  // 如果指定了非草稿状态，需要在编辑模式下修改（这里先创建草稿）
  // 提交表单
  const submitButton = page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  await submitButton.click();

  // 等待网络请求完成
  try {
    await page.waitForLoadState('networkidle', { timeout: TIMEOUTS.DIALOG });
  } catch {
    console.debug('networkidle 超时，继续检查 Toast 消息');
  }

  // 等待 Toast 消息出现（成功或错误）
  const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  await successToast.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT })
    .catch(() => console.debug('未检测到成功 Toast，继续检查'));

  // 检查是否有成功消息
  const hasSuccess = await successToast.count() > 0;

  // 等待对话框关闭
  const dialog = page.locator('[role="dialog"]');
  await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.DIALOG }).catch(() => {
    console.debug('对话框关闭超时，可能已经关闭');
  });

  return hasSuccess;
}

test.describe('订单状态流转测试', () => {
  test.describe('激活草稿订单', () => {
    let draftOrderName: string;

    test.beforeEach(async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      // 以管理员身份登录后台
      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      // 创建一个草稿状态的测试订单
      const timestamp = Date.now();
      draftOrderName = `激活测试_${timestamp}`;

      const created = await createTestOrder(orderManagementPage.page, draftOrderName);

      if (!created) {
        test.skip(true, '没有残疾人数据，无法创建测试订单');
      }

      // 验证订单出现在列表中
      await expect(async () => {
        const exists = await orderManagementPage.orderExists(draftOrderName);
        expect(exists).toBe(true);
      }).toPass({ timeout: TIMEOUTS.DIALOG });

      // 验证初始状态为草稿
      const initialStatus = await orderManagementPage.getOrderStatus(draftOrderName);
      console.debug(`✓ 订单初始状态: ${initialStatus ? ORDER_STATUS_LABELS[initialStatus] : '未知'}`);
    });

    test('应该成功激活草稿订单', async ({ orderManagementPage }) => {
      // 执行激活操作
      const success = await orderManagementPage.activateOrder(draftOrderName);

      // 验证激活成功
      expect(success).toBe(true);

      // 验证状态从草稿变为进行中
      await expect(async () => {
        const currentStatus = await orderManagementPage.getOrderStatus(draftOrderName);
        expect(currentStatus).toBe(ORDER_STATUS.IN_PROGRESS);
      }).toPass({ timeout: TIMEOUTS.DIALOG });
    });

    test('激活后应该显示成功提示', async ({ orderManagementPage }) => {
      await orderManagementPage.activateOrder(draftOrderName);

      // 验证 Toast 成功消息
      const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
      await expect(successToast).toBeVisible();
      const message = await successToast.textContent();
      expect(message).toBeDefined();
      expect(message?.length).toBeGreaterThan(0);
      // 验证消息包含激活相关的关键词（更精确的正则）
      expect(message).toMatch(/激活.*成功|已激活/);
    });

    test('激活确认对话框应该正确显示', async ({ orderManagementPage, page }) => {
      await orderManagementPage.openActivateDialog(draftOrderName);

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

      // 验证确认按钮存在（支持多种可能的按钮名称）
      const confirmButton = page.locator('[role="alertdialog"]').getByRole('button', {
        name: /^(确认激活|激活|确定|确认)$/
      });
      await expect(confirmButton).toBeVisible();

      // 验证取消按钮存在
      const cancelButton = page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
      await expect(cancelButton).toBeVisible();

      // 取消激活以清理
      await page.keyboard.press('Escape');
    });
  });

  test.describe('关闭进行中订单', () => {
    let inProgressOrderName: string;

    test.beforeEach(async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      // 以管理员身份登录后台
      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      // 创建一个草稿订单然后激活为进行中状态
      const timestamp = Date.now();
      inProgressOrderName = `关闭测试_${timestamp}`;

      const created = await createTestOrder(orderManagementPage.page, inProgressOrderName);

      if (!created) {
        test.skip(true, '没有残疾人数据，无法创建测试订单');
      }

      // 验证订单出现在列表中
      await expect(async () => {
        const exists = await orderManagementPage.orderExists(inProgressOrderName);
        expect(exists).toBe(true);
      }).toPass({ timeout: TIMEOUTS.DIALOG });

      // 激活订单到进行中状态
      await orderManagementPage.activateOrder(inProgressOrderName);

      // 验证状态变为进行中
      await expect(async () => {
        const currentStatus = await orderManagementPage.getOrderStatus(inProgressOrderName);
        expect(currentStatus).toBe(ORDER_STATUS.IN_PROGRESS);
      }).toPass({ timeout: TIMEOUTS.DIALOG });

      console.debug(`✓ 订单已激活为进行中状态`);
    });

    test('应该成功关闭进行中订单', async ({ orderManagementPage }) => {
      // 执行关闭操作
      const success = await orderManagementPage.closeOrder(inProgressOrderName);

      // 验证关闭成功
      expect(success).toBe(true);

      // 验证状态从进行中变为已完成
      await expect(async () => {
        const currentStatus = await orderManagementPage.getOrderStatus(inProgressOrderName);
        expect(currentStatus).toBe(ORDER_STATUS.COMPLETED);
      }).toPass({ timeout: TIMEOUTS.DIALOG });
    });

    test('关闭后应该显示成功提示', async ({ orderManagementPage }) => {
      await orderManagementPage.closeOrder(inProgressOrderName);

      // 验证 Toast 成功消息
      const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
      await expect(successToast).toBeVisible();
      const message = await successToast.textContent();
      expect(message).toBeDefined();
      expect(message?.length).toBeGreaterThan(0);
      // 验证消息包含关闭相关的关键词（更精确的正则）
      expect(message).toMatch(/关闭.*成功|已完成/);
    });

    test('关闭确认对话框应该正确显示', async ({ orderManagementPage, page }) => {
      await orderManagementPage.openCloseDialog(inProgressOrderName);

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

      // 验证确认按钮存在（支持多种可能的按钮名称）
      const confirmButton = page.locator('[role="alertdialog"]').getByRole('button', {
        name: /^(确认关闭|关闭|确定|确认)$/
      });
      await expect(confirmButton).toBeVisible();

      // 验证取消按钮存在
      const cancelButton = page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
      await expect(cancelButton).toBeVisible();

      // 取消关闭以清理
      await page.keyboard.press('Escape');
    });
  });

  test.describe('状态限制验证', () => {
    let shouldSkipTests = false;

    // 创建测试订单的辅助函数
    async function setupTestOrder(page: Page, orderName: string): Promise<boolean> {
      const created = await createTestOrder(page, orderName);
      if (!created) {
        shouldSkipTests = true;
        return false;
      }

      // 等待订单出现在列表中（使用条件等待而非硬编码超时）
      try {
        await page.waitForFunction(
          (name) => {
            const table = document.querySelector('table tbody tr');
            if (!table) return false;
            const rows = Array.from(document.querySelectorAll('table tbody tr'));
            return rows.some(row => row.textContent?.includes(name));
          },
          orderName,
          { timeout: TIMEOUTS.DIALOG }
        );
      } catch {
        console.debug(`订单 ${orderName} 未在列表中找到`);
      }
      return true;
    }

    test('不能激活非草稿状态的订单', async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      if (shouldSkipTests) {
        test.skip(true, '测试数据不可用');
        return;
      }

      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      // 测试 IN_PROGRESS 状态不能激活
      const inProgressOrderName = `限制测试_进行中_${Date.now()}`;
      if (await setupTestOrder(orderManagementPage.page, inProgressOrderName)) {
        await orderManagementPage.activateOrder(inProgressOrderName);

        const isActivateEnabled = await orderManagementPage.checkActivateButtonEnabled(inProgressOrderName);
        expect(isActivateEnabled).toBe(false);
        console.debug(`✓ IN_PROGRESS 状态的订单不能激活`);
      }

      // 测试 COMPLETED 状态不能激活
      const completedOrderName = `限制测试_已完成_${Date.now()}`;
      if (await setupTestOrder(orderManagementPage.page, completedOrderName)) {
        await orderManagementPage.activateOrder(completedOrderName);
        await orderManagementPage.closeOrder(completedOrderName);

        const isActivateEnabled = await orderManagementPage.checkActivateButtonEnabled(completedOrderName);
        expect(isActivateEnabled).toBe(false);
        console.debug(`✓ COMPLETED 状态的订单不能激活`);
      }
    });

    test('不能关闭非进行中状态的订单', async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      if (shouldSkipTests) {
        test.skip(true, '测试数据不可用');
        return;
      }

      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      // 测试 DRAFT 状态不能关闭
      const draftOrderName = `限制测试_草稿_${Date.now()}`;
      if (await setupTestOrder(orderManagementPage.page, draftOrderName)) {
        const isCloseEnabled = await orderManagementPage.checkCloseButtonEnabled(draftOrderName);
        expect(isCloseEnabled).toBe(false);
        console.debug(`✓ DRAFT 状态的订单不能关闭`);
      }

      // 测试 COMPLETED 状态不能关闭
      const completedOrderName = `限制测试_已完成_2_${Date.now()}`;
      if (await setupTestOrder(orderManagementPage.page, completedOrderName)) {
        await orderManagementPage.activateOrder(completedOrderName);
        await orderManagementPage.closeOrder(completedOrderName);

        const isCloseEnabled = await orderManagementPage.checkCloseButtonEnabled(completedOrderName);
        expect(isCloseEnabled).toBe(false);
        console.debug(`✓ COMPLETED 状态的订单不能关闭`);
      }
    });

    test('已完成状态不能再次操作', async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      if (shouldSkipTests) {
        test.skip(true, '测试数据不可用');
        return;
      }

      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      const orderName = `限制测试_已完成操作_${Date.now()}`;
      if (!await setupTestOrder(orderManagementPage.page, orderName)) {
        test.skip(true, '测试数据不可用');
        return;
      }

      // 操作到已完成状态
      await orderManagementPage.activateOrder(orderName);
      await orderManagementPage.closeOrder(orderName);

      // 验证订单状态为已完成
      await expect(async () => {
        const status = await orderManagementPage.getOrderStatus(orderName);
        expect(status).toBe(ORDER_STATUS.COMPLETED);
      }).toPass({ timeout: TIMEOUTS.DIALOG });

      // 已完成状态的订单不能激活也不能关闭
      const isActivateEnabled = await orderManagementPage.checkActivateButtonEnabled(orderName);
      const isCloseEnabled = await orderManagementPage.checkCloseButtonEnabled(orderName);

      expect(isActivateEnabled).toBe(false);
      expect(isCloseEnabled).toBe(false);
      console.debug(`✓ 已完成状态的订单不能激活或关闭`);
    });
  });

  test.describe('完整状态流转流程', () => {
    test('应该支持草稿→进行中→已完成的完整流转', async ({ adminLoginPage, orderManagementPage, testUsers }) => {
      // 以管理员身份登录后台
      await adminLoginPage.goto();
      await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
      await adminLoginPage.expectLoginSuccess();
      await orderManagementPage.goto();

      // 创建草稿订单
      const timestamp = Date.now();
      const orderName = `完整流转测试_${timestamp}`;

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

      if (!created) {
        test.skip(true, '没有残疾人数据，无法创建测试订单');
      }

      // 验证订单出现在列表中
      await expect(async () => {
        const exists = await orderManagementPage.orderExists(orderName);
        expect(exists).toBe(true);
      }).toPass({ timeout: TIMEOUTS.DIALOG });

      // 验证初始状态为草稿
      let currentStatus = await orderManagementPage.getOrderStatus(orderName);
      expect(currentStatus).toBe(ORDER_STATUS.DRAFT);
      console.debug(`✓ 初始状态: ${ORDER_STATUS_LABELS[ORDER_STATUS.DRAFT]}`);

      // 激活订单
      await orderManagementPage.activateOrder(orderName);
      currentStatus = await orderManagementPage.getOrderStatus(orderName);
      expect(currentStatus).toBe(ORDER_STATUS.IN_PROGRESS);
      console.debug(`✓ 激活后状态: ${ORDER_STATUS_LABELS[ORDER_STATUS.IN_PROGRESS]}`);

      // 关闭订单
      await orderManagementPage.closeOrder(orderName);
      currentStatus = await orderManagementPage.getOrderStatus(orderName);
      expect(currentStatus).toBe(ORDER_STATUS.COMPLETED);
      console.debug(`✓ 关闭后状态: ${ORDER_STATUS_LABELS[ORDER_STATUS.COMPLETED]}`);
    });
  });
});
