import { test, expect } from "@playwright/test";
import { USERS, login } from "./helpers";
import { approvedPo, closeDb } from "./fixtures";

/**
 * Issue #53 — Managers can cancel a PO via a confirmation modal that requires a
 * reason and typing the word "cancel". This spec verifies the affordance and the
 * type-to-confirm guard WITHOUT actually cancelling (non-destructive on staging
 * data): it confirms the submit button stays disabled until "cancel" is typed, then
 * closes the modal.
 */
test.afterAll(closeDb);

test("#53 Cancel PO modal enforces type-'cancel'-to-confirm", async ({ page }) => {
  const po = await approvedPo();
  test.skip(!po, "no approved PO in staging data to exercise the cancel control");

  await login(page, USERS.MANAGER);
  await page.goto(`/po/${po!.id}`);

  await page.getByRole("button", { name: "Cancel PO" }).click();

  const submit = page.getByRole("button", { name: "Cancel this PO" });
  await expect(submit).toBeVisible();
  await expect(submit).toBeDisabled();

  // A reason alone is not enough.
  await page.getByPlaceholder(/Duplicate order/i).fill("Verification test — not a real cancellation");
  await expect(submit).toBeDisabled();

  // Typing the confirmation word enables it.
  await page.getByPlaceholder("cancel").fill("cancel");
  await expect(submit).toBeEnabled();

  // Back out without cancelling.
  await page.getByRole("button", { name: "Keep PO" }).click();
  await expect(submit).toBeHidden();
});
