/**
 * COMPANY_7 사용자(topseal_admin) 발주관리 결재 시스템 테스트
 * 실행: npx tsx frontend/scripts/po-approval-company7-test.ts
 */
import { chromium } from "playwright";
import { writeFileSync } from "fs";

const BASE_URL = "http://localhost:9771";
const LOGIN_ID = "topseal_admin";
const LOGIN_PW = "qlalfqjsgh11";
const SCREEN_URL = `${BASE_URL}/screen/COMPANY_7_064`;

const results: string[] = [];
const screenshotDir = "/Users/gbpark/ERP-node/approval-company7-screenshots";

async function main() {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({ viewport: { width: 1280, height: 900 } });
  const page = await context.newPage();

  const screenshot = async (name: string) => {
    const path = `${screenshotDir}/${name}.png`;
    await page.screenshot({ path, fullPage: true });
    results.push(`  스크린샷: ${name}.png`);
  };

  try {
    // Step 1: 로그인
    results.push("\n=== Step 1: 로그인 (topseal_admin) ===");
    await page.goto(BASE_URL, { waitUntil: "domcontentloaded", timeout: 30000 });
    await page.waitForTimeout(2000);

    const loginPage = page.locator('input[type="text"], input[name="userId"], #userId').first();
    if ((await loginPage.count()) > 0) {
      await page.getByPlaceholder("사용자 ID를 입력하세요").or(page.locator('#userId, input[name="userId"]')).first().fill(LOGIN_ID);
      await page.getByPlaceholder("비밀번호를 입력하세요").or(page.locator('#password, input[name="password"]')).first().fill(LOGIN_PW);
      await page.getByRole("button", { name: "로그인" }).or(page.locator('button[type="submit"]')).first().click();
      await page.waitForTimeout(3000);
      try {
        await page.waitForURL((url) => !url.toString().includes("/login"), { timeout: 25000 });
      } catch {
        results.push("  WARN: 로그인 후 URL 변경 없음 - 로그인 실패 가능");
      }
    }
    await page.waitForTimeout(3000);
    const urlAfterLogin = page.url();
    results.push(`  현재 URL: ${urlAfterLogin}`);
    await screenshot("01-after-login");
    if (urlAfterLogin.includes("/login")) {
      results.push("  FAIL: 로그인 실패 - 여전히 로그인 페이지에 있음");
    } else {
      results.push("  OK: 로그인 완료");
    }

    // Step 2: 구매관리 메뉴 또는 직접 URL
    results.push("\n=== Step 2: 발주관리 화면 이동 ===");
    const purchaseMenu = page.locator('text="구매관리"').first();
    const hasPurchaseMenu = (await purchaseMenu.count()) > 0;
    if (hasPurchaseMenu) {
      await purchaseMenu.click();
      await page.waitForTimeout(800);
      const poMenu = page.locator('text="발주관리"').or(page.locator('text="발주 관리"')).first();
      if ((await poMenu.count()) > 0) {
        await poMenu.click();
        await page.waitForTimeout(3000);
      } else {
        await page.goto(SCREEN_URL, { waitUntil: "domcontentloaded", timeout: 20000 });
        await page.waitForTimeout(5000);
      }
    } else {
      results.push("  INFO: 구매관리 메뉴 없음, 직접 URL 이동");
      await page.goto(SCREEN_URL, { waitUntil: "domcontentloaded", timeout: 20000 });
      await page.waitForTimeout(5000);
    }
    await screenshot("02-po-screen");
    results.push("  OK: 발주관리 화면 로드");

    // Step 3: 그리드 컬럼 상세 확인
    results.push("\n=== Step 3: 그리드 컬럼 및 데이터 확인 ===");
    await page.waitForTimeout(2000);

    const headers = await page.locator("table th, [role='columnheader']").allTextContents();
    const headerTexts = headers.map((h) => h.trim()).filter((h) => h.length > 0);
    results.push(`  컬럼 헤더 (전체): ${JSON.stringify(headerTexts)}`);

    const firstCol = headerTexts[0] || "";
    const isFirstColKorean = firstCol === "결재상태";
    const isFirstColEnglish = firstCol === "approval_status" || firstCol.toLowerCase().includes("approval");
    results.push(`  첫 번째 컬럼: "${firstCol}"`);
    results.push(isFirstColKorean ? "  결재상태(한글) 표시됨" : isFirstColEnglish ? "  approval_status(영문) 표시됨" : `  기타: ${firstCol}`);

    const rows = await page.locator("table tbody tr, [role='row']").count();
    const hasEmptyMsg = (await page.locator('text="데이터가 없습니다"').count()) > 0;
    results.push(`  데이터 행 수: ${rows}`);
    results.push(hasEmptyMsg ? "  빈 그리드: '데이터가 없습니다' 메시지 표시" : "  데이터 있음");

    if (rows > 0 && !hasEmptyMsg) {
      const firstColCells = await page.locator("table tbody tr td:first-child").allTextContents();
      results.push(`  첫 번째 컬럼 값(샘플): ${JSON.stringify(firstColCells.slice(0, 5))}`);

      const poNumbers = await page.locator("table tbody td").filter({ hasText: /PO-|발주/ }).allTextContents();
      results.push(`  발주번호 형식 데이터: ${poNumbers.length > 0 ? JSON.stringify(poNumbers.slice(0, 5)) : "없음"}`);
    }

    await screenshot("03-grid-detail");
    results.push("  OK: 그리드 상세 스크린샷 저장");

    // Step 4: 결재 요청 버튼 확인
    results.push("\n=== Step 4: 결재 요청 버튼 확인 ===");
    const approvalBtn = page.getByRole("button", { name: "결재 요청" }).or(page.locator('button:has-text("결재 요청")'));
    const hasApprovalBtn = (await approvalBtn.count()) > 0;
    results.push(hasApprovalBtn ? "  OK: '결재 요청' 파란색 버튼 확인됨" : "  FAIL: '결재 요청' 버튼 없음");
    await screenshot("04-approval-button");

    // Step 5: 행 선택 후 결재 요청 클릭
    results.push("\n=== Step 5: 행 선택 후 결재 요청 ===");
    const firstRow = page.locator("table tbody tr").first();
    const checkbox = page.locator("table tbody tr input[type='checkbox']").first();
    const hasRows = (await firstRow.count()) > 0;
    const hasCheckbox = (await checkbox.count()) > 0;

    if (hasRows) {
      if (hasCheckbox) {
        await checkbox.click();
        await page.waitForTimeout(300);
      } else {
        await firstRow.click();
        await page.waitForTimeout(300);
      }
      results.push("  OK: 행 선택 완료");
    } else {
      results.push("  INFO: 데이터 행 없음, 행 선택 없이 진행");
    }

    if (hasApprovalBtn) {
      await approvalBtn.first().click({ force: true });
      await page.waitForTimeout(2000);
      await screenshot("05-approval-modal");

      const modal = page.locator('[role="dialog"]');
      const modalOpened = (await modal.count()) > 0;
      results.push(modalOpened ? "  OK: 결재 모달 열림" : "  FAIL: 결재 모달 열리지 않음");

      if (modalOpened) {
        const searchInput = page.getByPlaceholder("이름 또는 사번으로 검색...").or(page.locator('[role="dialog"] input[placeholder*="검색"]'));
        if ((await searchInput.count()) > 0) {
          await searchInput.first().fill("김");
          await page.waitForTimeout(2000);
          await screenshot("06-approver-search-results");

          const searchResults = page.locator('[role="dialog"] div.max-h-48 button, [role="dialog"] div.overflow-y-auto button');
          const resultCount = await searchResults.count();
          const resultTexts = await searchResults.allTextContents();
          results.push(`  결재자 검색 결과: ${resultCount}명`);
          if (resultTexts.length > 0) {
            results.push(`  결재자 목록: ${JSON.stringify(resultTexts.slice(0, 10))}`);
          }
        }
      }
    }

    await screenshot("07-final");
  } catch (err: any) {
    results.push(`\nERROR: ${err.message}`);
    await page.screenshot({ path: `${screenshotDir}/error.png`, fullPage: true }).catch(() => {});
  } finally {
    await browser.close();
  }

  const output = results.join("\n");
  console.log("\n" + "=".repeat(60));
  console.log("COMPANY_7 (topseal_admin) 발주관리 결재 테스트 결과");
  console.log("=".repeat(60));
  console.log(output);
  console.log("=".repeat(60));
  writeFileSync("/Users/gbpark/ERP-node/approval-company7-report.txt", output);
}

main();
