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

/**
 * Issue #6 — Closed PO list filters.
 *  - MANAGER: the "Closed Purchase Orders" view shows ALL closed POs.
 *  - Submitter: the view shows ONLY CLOSED (no APPROVED leaking in).
 * Route: /my-orders.
 */
test.afterAll(closeDb);

test("#6 manager sees ALL closed POs on /my-orders", async ({ page }) => {
  const closed = await closedPoNumbers();
  test.skip(closed.length === 0, "no closed POs in staging data");

  await login(page, USERS.MANAGER);
  await page.goto("/my-orders");
  await expect(page.getByRole("heading", { name: "Closed Purchase Orders" })).toBeVisible();

  // The manager sees the FULL closed set (not just their own): every closed PO number
  // is present, and no APPROVED status leaks into this view.
  for (const poNumber of closed) {
    await expect(page.getByText(poNumber).first()).toBeVisible();
  }
  await expect(page.getByText("Approved", { exact: true })).toHaveCount(0);
});

test("#6 submitter's closed view excludes APPROVED POs", async ({ page }) => {
  await login(page, USERS.TECH);
  await page.goto("/my-orders");
  await expect(page.getByRole("heading", { name: "Closed Purchase Orders" })).toBeVisible();
  // The bug was APPROVED POs showing here; assert none do.
  await expect(page.getByText("Approved", { exact: true })).toHaveCount(0);
});
