/**
 * User stories covered: Feature 7 — Payment history page at /payments/history
 *   - ACCOUNTS user can navigate to /payments/history and see paid POs
 *   - Non-ACCOUNTS roles are redirected away from /payments/history
 *
 * Note: /payments/history uses hasPermission(role, "view_all_pos") — only ACCOUNTS,
 * MANAGER, SUPERUSER, AUDITOR, ADMIN have this permission. TECHNICAL does not.
 *
 * Created: 2026-05-17
 */
import { test, expect } from "@playwright/test";
import { login, USERS } from "./helpers/login";

test.describe("Feature 7 — Payment history page", () => {
  test("US-7a: ACCOUNTS user can load /payments/history", async ({ page }) => {
    await login(page, USERS.ACCOUNTS);
    await page.goto("/payments/history");

    await expect(page).toHaveURL(/payments\/history/);
    await expect(
      page.getByRole("heading", { name: /payment history/i })
    ).toBeVisible();
    console.log("✓ Payment History page loads for ACCOUNTS user");
  });

  test("US-7a: /payments/history shows a table or empty-state message", async ({
    page,
  }) => {
    await login(page, USERS.ACCOUNTS);
    await page.goto("/payments/history");

    // Either a table with rows, or the empty-state paragraph
    const tableOrEmpty =
      (await page.locator("table").count()) > 0 ||
      (await page.getByText(/no paid orders found/i).isVisible());

    expect(tableOrEmpty).toBeTruthy();
    console.log("✓ Payment History renders table or empty-state");
  });

  test("US-7a: /payments/history contains a heading or summary stat", async ({
    page,
  }) => {
    await login(page, USERS.ACCOUNTS);
    await page.goto("/payments/history");

    // Page shows "Payment History" heading and a "Total Paid" stat card
    await expect(page.getByText(/total paid/i)).toBeVisible();
    console.log("✓ Total Paid stat visible on Payment History page");
  });

  test("US-7b: TECHNICAL user is redirected away from /payments/history", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    await page.goto("/payments/history");

    // The page redirects non-view_all_pos roles to /dashboard
    await expect(page).not.toHaveURL(/payments\/history/);
    console.log("✓ TECHNICAL user redirected from /payments/history");
  });

  test("US-7b: MANNING user is redirected away from /payments/history", async ({
    page,
  }) => {
    await login(page, USERS.MANNING);
    await page.goto("/payments/history");

    await expect(page).not.toHaveURL(/payments\/history/);
    console.log("✓ MANNING user redirected from /payments/history");
  });

  test("US-7a: MANAGER user can also access /payments/history (view_all_pos)", async ({
    page,
  }) => {
    await login(page, USERS.MANAGER);
    await page.goto("/payments/history");
    // Manager has view_all_pos permission — should NOT be redirected
    await expect(page).toHaveURL(/payments\/history/);
    await expect(
      page.getByRole("heading", { name: /payment history/i })
    ).toBeVisible();
    console.log("✓ MANAGER can access Payment History page");
  });
});
