import type { Department, Operation } from "./format";

/**
 * Which department each page belongs to. Used twice, so the sidebar and the request gate
 * in `src/proxy.ts` can never disagree: the sidebar hides what you may not view, and the
 * gate refuses the same pages when the URL is typed by hand.
 */
export const PAGES: Array<{ href: string; label: string; group: string; department: Department }> = [
  { href: "/", label: "Overview", department: "statistics", group: "Home" },
  { href: "/customers", label: "Customers", department: "customers", group: "Sales" },
  { href: "/products", label: "Categories & products", department: "products", group: "Sales" },
  { href: "/pricing", label: "Pricing", department: "pricing", group: "Sales" },
  { href: "/orders", label: "Orders", department: "orders", group: "Sales" },
  { href: "/stock", label: "Stock movements", department: "products", group: "Stock & purchasing" },
  { href: "/purchases", label: "Purchases", department: "purchases", group: "Stock & purchasing" },
  { href: "/suppliers", label: "Suppliers", department: "purchases", group: "Stock & purchasing" },
  { href: "/store", label: "Point of sale", department: "store", group: "Internal store" },
  { href: "/store/sales", label: "Sales receipts", department: "store", group: "Internal store" },
  { href: "/store/expenses", label: "Expenses", department: "store", group: "Internal store" },
  { href: "/store/stock", label: "Store stock", department: "store", group: "Internal store" },
  { href: "/rounds", label: "Rounds", department: "rounds", group: "Field" },
  { href: "/fleet", label: "Drivers", department: "fleet", group: "Field" },
  { href: "/delivery-notes", label: "Delivery notes", department: "invoices", group: "Field" },
  { href: "/invoices", label: "Proformas", department: "invoices", group: "Finance" },
  { href: "/payments", label: "Payments", department: "payments", group: "Finance" },
  { href: "/settings", label: "Document settings", department: "settings", group: "Finance" },
  { href: "/statistics", label: "Statistics", department: "statistics", group: "Insights" },
  { href: "/analysis", label: "Data analysis", department: "analysis", group: "Insights" },
  { href: "/activity", label: "Activity log", department: "activity", group: "Insights" },
  { href: "/notifications", label: "Notifications", department: "notifications", group: "Insights" },
  { href: "/users", label: "Users & roles", department: "users", group: "Admin" },
];

/** Pages everyone may read whatever their role: they hold no data of their own. */
export const OPEN_PAGES: Array<{ href: string; label: string; group: string }> = [
  { href: "/guide", label: "User guide", group: "Help" },
];

/** Printable documents and CSV exports follow the department of the data they show. */
const EXTRA_PATHS: Array<{ prefix: string; department: Department }> = [
  { prefix: "/print/proforma", department: "invoices" },
  { prefix: "/print/delivery", department: "invoices" },
  { prefix: "/print/round", department: "rounds" },
  { prefix: "/print/store-sale", department: "store" },
  { prefix: "/api/export/activity", department: "activity" },
  { prefix: "/api/export/payments", department: "payments" },
  { prefix: "/api/export/stats", department: "statistics" },
  { prefix: "/api/export/analysis", department: "analysis" },
  { prefix: "/api/export/stock", department: "products" },
];

export function departmentForPath(pathname: string): Department | null {
  // Open pages belong to no department, so the gate in `src/proxy.ts` lets them through.
  if (OPEN_PAGES.some((page) => pathname === page.href || pathname.startsWith(`${page.href}/`))) return null;
  for (const { prefix, department } of EXTRA_PATHS) {
    if (pathname === prefix || pathname.startsWith(`${prefix}/`)) return department;
  }
  const page = PAGES.filter((item) => item.href !== "/")
    .find((item) => pathname === item.href || pathname.startsWith(`${item.href}/`));
  if (page) return page.department;
  return pathname === "/" ? "statistics" : null;
}

export function mayView(
  permissions: Record<string, Operation[] | undefined>,
  roleName: string,
  department: Department,
) {
  if (roleName === "Super Admin") return true;
  return permissions[department]?.includes("view") ?? false;
}

/** Where to send someone who opened a page they may not view. */
export function firstAllowedPath(permissions: Record<string, Operation[] | undefined>, roleName: string) {
  return PAGES.find((page) => mayView(permissions, roleName, page.department))?.href ?? null;
}
