import { test as base } from "@playwright/test";
import { createUser } from "../utils/user-factory";

export type Actor = "product-manager" | "viewer" | "user";

const ACTOR_PERMISSIONS: Record<Actor, string[]> = {
  "product-manager": [
    "product-management:product",
    "product-management:productCatalog",
    "user-management:user",
    "user-management:role",
    "user-management:roleAssignment",
    "user-management:profile",
    "audit:auditEntry",
  ],
  viewer: ["product-management:product", "product-management:productCatalog"],
  user: [
    "user-management:user",
    "user-management:role",
    "user-management:roleAssignment",
    "user-management:profile",
    "audit:auditEntry",
  ],
};

type UserFixture = Awaited<ReturnType<typeof createUser>>;

interface WorkerFixtures {
  ProductManager: UserFixture;
  Viewer: UserFixture;
  User: UserFixture;
}

export const test = base.extend<object, WorkerFixtures>({
  ProductManager: [
    // oxlint-disable-next-line no-empty-pattern
    async ({}, use) => {
      const user = await createUser({ permissions: ACTOR_PERMISSIONS["product-manager"] });
      await use(user);
    },
    { scope: "worker" },
  ],
  Viewer: [
    // oxlint-disable-next-line no-empty-pattern
    async ({}, use) => {
      const user = await createUser({ permissions: ACTOR_PERMISSIONS.viewer });
      await use(user);
    },
    { scope: "worker" },
  ],
  User: [
    // oxlint-disable-next-line no-empty-pattern
    async ({}, use) => {
      const user = await createUser({ permissions: ACTOR_PERMISSIONS.user });
      await use(user);
    },
    { scope: "worker" },
  ],
});

export const expect = test.expect;
