export const appRoles = [
  "ADMIN",
  "SYSTEM_ADMIN",
  "OWNER",
  "PURCHASING",
  "WAREHOUSE",
  "QC",
  "SALES"
] as const;

export type AppRole = (typeof appRoles)[number];

type AccessRule = {
  prefix: string;
  read: AppRole[];
  write?: AppRole[];
};

const fullAccessRoles: AppRole[] = ["ADMIN", "SYSTEM_ADMIN"];
const operationalRoles: AppRole[] = [
  "OWNER",
  "PURCHASING",
  "WAREHOUSE",
  "QC",
  "SALES"
];

export const pageAccessRules: AccessRule[] = [
  { prefix: "/dashboard", read: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/change-password", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/help", read: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/users", read: [...fullAccessRoles], write: [...fullAccessRoles] },
  { prefix: "/audit-trail", read: [...fullAccessRoles], write: [...fullAccessRoles] },
  { prefix: "/settings", read: [...fullAccessRoles, "OWNER"], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/agents", read: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/sales", read: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/buyers", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/employees", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/grades", read: [...fullAccessRoles, "OWNER", "PURCHASING", "QC", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "QC", "SALES"] },
  { prefix: "/warehouses", read: [...fullAccessRoles, "OWNER", "WAREHOUSE"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/warehouse-locations", read: [...fullAccessRoles, "OWNER", "WAREHOUSE"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/adjustment-reasons", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"] },
  { prefix: "/stock-adjustments", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"] },
  { prefix: "/banks", read: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/couriers", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"] },
  { prefix: "/washing-places", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"] },
  { prefix: "/currencies", read: [...fullAccessRoles, "OWNER"], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/profit-share-schemes", read: [...fullAccessRoles, "OWNER"], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/units", read: [...fullAccessRoles, "OWNER"], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/purchases", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/fund-requests", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/washing", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"] },
  { prefix: "/purchase-analysis", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/purchase-realization", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/receipts", read: [...fullAccessRoles, "OWNER", "WAREHOUSE"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/lots", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC", "SALES"] },
  { prefix: "/sorting", read: [...fullAccessRoles, "OWNER", "QC"], write: [...fullAccessRoles, "OWNER", "QC"] },
  { prefix: "/sales", read: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/sales-allocation", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/sales-regular", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/sales-jit", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/consignments", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"] },
  { prefix: "/barcode/lookup", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC", "SALES"] },
  { prefix: "/reports/stock-summary", read: [...fullAccessRoles, ...operationalRoles] }
];

export const apiAccessRules: AccessRule[] = [
  { prefix: "/api/v1/auth/change-password", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/api/v1/settings", read: [...fullAccessRoles, "OWNER"], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/api/v1/agents", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/api/v1/sales", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/api/v1/users", read: [...fullAccessRoles], write: [...fullAccessRoles] },
  { prefix: "/api/v1/audit-trail", read: [...fullAccessRoles], write: [...fullAccessRoles] },
  { prefix: "/api/v1/buyers", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/api/v1/employees", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/api/v1/grades", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "QC", "SALES"] },
  { prefix: "/api/v1/warehouses", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/api/v1/warehouse-locations", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/api/v1/adjustment-reasons", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/stock-adjustments", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/banks", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "SALES"] },
  { prefix: "/api/v1/couriers", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"] },
  { prefix: "/api/v1/washing-places", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/currencies", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/api/v1/profit-share-schemes", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/api/v1/units", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, "OWNER"] },
  { prefix: "/api/v1/purchases", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/fund-requests", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/washing", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/purchase-analyses", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/purchase-realizations", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/receipts", read: [...fullAccessRoles, "OWNER", "WAREHOUSE"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/api/v1/lots", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC", "SALES"] },
  { prefix: "/api/v1/lot-transformations", read: [...fullAccessRoles, "OWNER", "QC"], write: [...fullAccessRoles, "OWNER", "QC"] },
  { prefix: "/api/v1/sales-regular", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/api/v1/sales-jit", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/api/v1/consignments", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"] },
  { prefix: "/api/v1/mobile/dashboard", read: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/api/v1/mobile/bootstrap", read: [...fullAccessRoles, ...operationalRoles] },
  { prefix: "/api/v1/mobile/purchases", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/mobile/fund-requests", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/mobile/purchase-analyses", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/mobile/purchase-realizations", read: [...fullAccessRoles, "OWNER", "PURCHASING"], write: [...fullAccessRoles, "OWNER", "PURCHASING"] },
  { prefix: "/api/v1/mobile/receipts", read: [...fullAccessRoles, "OWNER", "WAREHOUSE"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE"] },
  { prefix: "/api/v1/mobile/lots", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC", "SALES"] },
  { prefix: "/api/v1/mobile/stock-adjustments", read: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/mobile/washing", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "QC"] },
  { prefix: "/api/v1/mobile/lot-transformations", read: [...fullAccessRoles, "OWNER", "QC"], write: [...fullAccessRoles, "OWNER", "QC"] },
  { prefix: "/api/v1/mobile/sales-regular", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/api/v1/mobile/sales-jit", read: [...fullAccessRoles, "OWNER", "SALES"], write: [...fullAccessRoles, "OWNER", "SALES"] },
  { prefix: "/api/v1/mobile/consignments", read: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"], write: [...fullAccessRoles, "OWNER", "PURCHASING", "WAREHOUSE", "SALES"] },
  { prefix: "/api/v1/mobile", read: [...fullAccessRoles, ...operationalRoles], write: [...fullAccessRoles, ...operationalRoles] }
];

export function isPrivilegedRole(role: string): role is AppRole {
  return appRoles.includes(role as AppRole);
}

export function getDefaultPathForRole(role: string) {
  switch (role) {
    case "PURCHASING":
      return "/purchases";
    case "WAREHOUSE":
      return "/lots";
    case "QC":
      return "/sorting";
    case "SALES":
      return "/sales";
    default:
      return "/dashboard";
  }
}

function findRule(pathname: string, rules: AccessRule[]) {
  return rules.find((rule) => pathname === rule.prefix || pathname.startsWith(`${rule.prefix}/`));
}

export function canAccessPage(role: string, pathname: string) {
  const rule = findRule(pathname, pageAccessRules);
  if (!rule) return fullAccessRoles.includes(role as AppRole);
  return rule.read.includes(role as AppRole);
}

export function canAccessApi(role: string, pathname: string, method: string) {
  const rule = findRule(pathname, apiAccessRules);
  if (!rule) return fullAccessRoles.includes(role as AppRole);

  const isReadMethod = method === "GET" || method === "HEAD";
  const allowedRoles = isReadMethod ? rule.read : (rule.write ?? rule.read);
  return allowedRoles.includes(role as AppRole);
}
