/**
 * User stories covered: Feature 11 — User profile page & manager signature
 *   - Any logged-in user can load /profile and see their name and role
 *   - MANAGER's profile page shows a signature field/upload area
 *   - Non-manager role (e.g., TECHNICAL) does NOT see the signature section
 *
 * Created: 2026-05-17
 */
import { test, expect } from "@playwright/test";
import { login, USERS } from "./helpers/login";

test.describe("Feature 11 — User profile page & manager signature", () => {
  test("US-11a: TECHNICAL user's /profile shows name and role", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    await page.goto("/profile");

    await expect(
      page.getByRole("heading", { name: /my profile/i })
    ).toBeVisible();
    // Profile page shows Account Information section — role badge in a <dd> span
    await expect(page.getByText("Name")).toBeVisible();
    await expect(page.getByText("Role")).toBeVisible();
    // Role badge is scoped to <dd> to avoid matching the header display
    await expect(page.locator("dd span").filter({ hasText: "Technical" })).toBeVisible();
    console.log("✓ TECHNICAL user profile shows name and role");
  });

  test("US-11a: ACCOUNTS user's /profile shows their role", async ({ page }) => {
    await login(page, USERS.ACCOUNTS);
    await page.goto("/profile");

    await expect(page.locator("dd span").filter({ hasText: "Accounts" })).toBeVisible();
    console.log("✓ ACCOUNTS user profile shows Accounts role");
  });

  test("US-11a: MANAGER user's /profile loads without error", async ({ page }) => {
    await login(page, USERS.MANAGER);
    await page.goto("/profile");

    await expect(
      page.getByRole("heading", { name: /my profile/i })
    ).toBeVisible();
    await expect(page.locator("dd span").filter({ hasText: "Manager" })).toBeVisible();
    console.log("✓ MANAGER profile page loads and shows Manager role");
  });

  test("US-11b: MANAGER profile page shows the Approval Signature section", async ({
    page,
  }) => {
    await login(page, USERS.MANAGER);
    await page.goto("/profile");

    // SignatureUploader section is only shown for MANAGER and SUPERUSER
    await expect(page.getByText(/approval signature/i)).toBeVisible();
    console.log("✓ Approval Signature section visible on Manager profile");
  });

  test("US-11b: TECHNICAL user profile does NOT show signature section", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    await page.goto("/profile");

    await expect(page.getByText(/approval signature/i)).not.toBeVisible();
    console.log("✓ Approval Signature section correctly absent for TECHNICAL user");
  });

  test("US-11b: SUPERUSER profile page also shows signature section", async ({
    page,
  }) => {
    await login(page, USERS.SUPERUSER);
    await page.goto("/profile");

    await expect(page.getByText(/approval signature/i)).toBeVisible();
    console.log("✓ Approval Signature section visible on SuperUser profile");
  });

  test("US-11a: profile page shows Change Password section for all users", async ({
    page,
  }) => {
    await login(page, USERS.TECH);
    await page.goto("/profile");

    await expect(
      page.locator("section h2").filter({ hasText: /change password/i })
    ).toBeVisible();
    console.log("✓ Change Password section visible on profile page");
  });
});
