import { test, expect } from '@playwright/test';
import { LoginPage } from '../../pages/LoginPage';
import { DashboardPage } from '../../pages/DashboardPage';

test.describe('业主报修全流程', () => {
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    await loginPage.goto();
    await loginPage.loginAsAdmin();
    await page.waitForURL('**/dashboard**', { timeout: 10000 });
  });

  test('TC-CROSS-001: 业主报修完整流程 - 从创建工单到验收', async ({ page }) => {
    await dashboardPage.navigateTo('工单管理');
    await page.waitForLoadState('networkidle');

    const createButton = page.locator('button:has-text("新建"), button:has-text("创建")');
    if (await createButton.isVisible()) {
      await createButton.click();
      await page.waitForLoadState('networkidle');

      const titleInput = page.locator('input[id="title"], input[placeholder*="标题"]');
      if (await titleInput.isVisible()) {
        await titleInput.fill('E2E测试-业主报修工单');
      }

      const descInput = page.locator('textarea[id="description"], textarea[placeholder*="描述"]');
      if (await descInput.isVisible()) {
        await descInput.fill('E2E自动化测试：业主报修全流程验证');
      }

      const submitButton = page.locator('button:has-text("确定"), button:has-text("提交")');
      if (await submitButton.isVisible()) {
        await submitButton.click();
        await page.waitForLoadState('networkidle');
      }
    }

    const workOrderRow = page.locator('tr:has-text("E2E测试-业主报修工单"), .ant-card:has-text("E2E测试")');
    if (await workOrderRow.isVisible()) {
      await expect(workOrderRow).toBeVisible();
    }
  });

  test('TC-CROSS-001-派单: 工单派单流程', async ({ page }) => {
    await dashboardPage.navigateTo('工单管理');
    await page.waitForLoadState('networkidle');

    const pendingOrder = page.locator('tr:has-text("待分配"), .ant-tag:has-text("待分配")').first();
    if (await pendingOrder.isVisible()) {
      await pendingOrder.click();
      await page.waitForLoadState('networkidle');

      const assignButton = page.locator('button:has-text("派单")');
      if (await assignButton.isVisible()) {
        await assignButton.click();
        await page.waitForLoadState('networkidle');
      }
    }
  });

  test('TC-CROSS-001-执行完成: 工单执行与完成', async ({ page }) => {
    await dashboardPage.navigateTo('工单管理');
    await page.waitForLoadState('networkidle');

    const assignedOrder = page.locator('tr:has-text("已派单"), .ant-tag:has-text("已派单")').first();
    if (await assignedOrder.isVisible()) {
      await assignedOrder.click();
      await page.waitForLoadState('networkidle');

      const startButton = page.locator('button:has-text("开始执行")');
      if (await startButton.isVisible()) {
        await startButton.click();
        await page.waitForLoadState('networkidle');
      }

      const completeButton = page.locator('button:has-text("完成")');
      if (await completeButton.isVisible()) {
        await completeButton.click();
        await page.waitForLoadState('networkidle');
      }
    }
  });

  test('TC-CROSS-001-验收: 工单验收流程', async ({ page }) => {
    await dashboardPage.navigateTo('工单管理');
    await page.waitForLoadState('networkidle');

    const completedOrder = page.locator('tr:has-text("已完成"), .ant-tag:has-text("已完成")').first();
    if (await completedOrder.isVisible()) {
      await completedOrder.click();
      await page.waitForLoadState('networkidle');

      const verifyButton = page.locator('button:has-text("验收")');
      if (await verifyButton.isVisible()) {
        await verifyButton.click();
        await page.waitForLoadState('networkidle');

        const ratingSelect = page.locator('.ant-rate, [data-testid="rating"]');
        if (await ratingSelect.isVisible()) {
          await ratingSelect.locator('.ant-rate-star').nth(4).click();
        }

        const confirmButton = page.locator('button:has-text("确认"), button:has-text("提交")');
        if (await confirmButton.isVisible()) {
          await confirmButton.click();
          await page.waitForLoadState('networkidle');
        }
      }
    }
  });
});
