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

/**
 * Issue #5 — Once a PO is approved, the PO date shown is the approval date
 * (when the submitter did not set an explicit poDate). Display rule:
 * poDisplayDate = po.poDate ?? po.approvedAt ?? po.createdAt.
 */
test.afterAll(closeDb);

test("#5 approved PO detail shows the approval date as the PO Date", async ({ page }) => {
  const po = await approvedPoNoPoDate();
  test.skip(!po, "no approved PO without an explicit poDate in staging data");

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

  await expect(page.getByText(po!.poNumber)).toBeVisible();
  // The "PO Date" detail row should render the approval date.
  const expected = formatDate(po!.approvedAt!);
  const row = page.getByText("PO Date", { exact: true }).locator("xpath=ancestor::*[1]");
  await expect(row).toContainText(expected);
});
