/**
 * User stories covered: Feature 3 — Submit for Approval button on DRAFT PO
 *   - TECHNICAL submitter sees "Submit for Approval" button on a DRAFT PO detail page
 *   - Button is NOT visible on a PO already in SUBMITTED/later status
 *
 * Created: 2026-05-17
 */
import { test, expect, type BrowserContext } from "@playwright/test";
import { login, USERS, createDraftPo, submitPo } from "./helpers/login";

test.describe("Feature 3 — Submit for Approval button on DRAFT PO", () => {
  test("US-3a: TECHNICAL user sees Submit for Approval button on a DRAFT PO", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    const poUrl = await createDraftPo(page, `E2E_DRAFT_BTN_${Date.now()}`);
    await page.goto(poUrl);

    // PoDetail renders SubmitDraftButton when status === DRAFT and own submitter
    const submitBtn = page.getByRole("button", { name: /submit for approval/i });
    await expect(submitBtn).toBeVisible();
    console.log("✓ Submit for Approval button visible on DRAFT PO detail");
  });

  test("US-3b: Submit for Approval button is absent on a SUBMITTED/MGR_REVIEW PO", async ({
    page,
    context,
  }: {
    page: import("@playwright/test").Page;
    context: BrowserContext;
  }) => {
    // Create and submit a PO as tech user
    await login(page, USERS.TECH);
    const poUrl = await submitPo(page, `E2E_SUBMITTED_BTN_${Date.now()}`);
    await page.goto(poUrl);

    // Reload as the same user — status is SUBMITTED or MGR_REVIEW
    const submitBtn = page.getByRole("button", { name: /submit for approval/i });
    await expect(submitBtn).not.toBeVisible();
    console.log("✓ Submit for Approval button NOT visible on submitted PO");
  });

  test("US-3a: submit button transitions DRAFT to Under Review", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    const poUrl = await createDraftPo(page, `E2E_SUBMIT_FLOW_${Date.now()}`);
    await page.goto(poUrl);

    await page.getByRole("button", { name: /submit for approval/i }).click();
    // After submission the status badge should change to Under Review / Submitted
    await expect(page.getByText(/under review|submitted/i)).toBeVisible({
      timeout: 10_000,
    });
    console.log("✓ PO transitions to Under Review after clicking Submit");
  });
});
