import type { Role } from "@prisma/client";

export const PERMISSIONS = [
  "dashboard.view",
  "products.view", "products.edit", "products.publish",
  "orders.view", "orders.fulfill", "orders.payments", "orders.cancel", "orders.export",
  "content.edit", "content.publish",
  "media.manage",
  "marketing.manage",
  "customers.view", "customers.export",
  "settings.manage",
  "team.manage",
  "audit.view",
] as const;
export type Permission = (typeof PERMISSIONS)[number];

const ROLE_PERMS: Record<Role, Permission[] | "*"> = {
  OWNER: "*",
  MANAGER: PERMISSIONS.filter((p) => p !== "team.manage" && p !== "settings.manage"),
  EDITOR: ["dashboard.view", "products.view", "products.edit", "content.edit", "content.publish", "media.manage", "marketing.manage"],
  FULFILLMENT: ["dashboard.view", "orders.view", "orders.fulfill", "products.view"],
  CUSTOMER: [],
};

export function can(role: Role | undefined | null, perm: Permission) {
  if (!role) return false;
  const p = ROLE_PERMS[role];
  return p === "*" || p.includes(perm);
}

export const STAFF_ROLES: Role[] = ["OWNER", "MANAGER", "EDITOR", "FULFILLMENT"];
export const isStaff = (role?: Role | null) => !!role && STAFF_ROLES.includes(role);
