import type { RoleCode } from "@/api/types";

/** System roles of the V2 access model (master plan §12.2). The server returns the real list from GET /roles. */
export const ROLE_LABELS: Record<RoleCode, string> = {
  system_admin: "System administrator",
  me_lead: "M&E lead",
  me_officer: "M&E officer",
  verification_supervisor: "Verification supervisor",
  verification_enumerator: "Verification enumerator",
  data_quality_officer: "Data-quality officer",
  funder_viewer: "Funder (read-only)",
  edi_viewer: "EDI (read-only)",
  tp_admin: "TP organisation admin",
  tp_site_lead: "TP site lead",
  tp_attendance_clerk: "TP attendance clerk",
  tp_outcomes_officer: "TP outcomes officer",
  tp_viewer: "TP viewer",
};
export const TP_ROLES: RoleCode[] = [
  "tp_admin",
  "tp_site_lead",
  "tp_attendance_clerk",
  "tp_outcomes_officer",
  "tp_viewer",
];
export const isTpRole = (r: RoleCode) => TP_ROLES.includes(r);
export const isVerificationRole = (r: RoleCode) =>
  r === "verification_supervisor" ||
  r === "verification_enumerator" ||
  r === "data_quality_officer";

/** Permission keys. The server decides what each person holds; the UI only hides what they cannot do. */
export const P = {
  projectView: "project.view",
  projectManage: "project.manage",
  settingsManage: "settings.manage",
  masterdataManage: "masterdata.manage",
  userView: "user.view",
  userManage: "user.manage",
  roleManage: "role.manage",
  auditView: "audit.view",
  auditExport: "audit.export",
  auditVerify: "audit.verify",
  providerView: "provider.view",
  providerManage: "provider.manage",
  contractManage: "provider.contract.manage",
  staffManage: "provider.staff.manage",
  targetView: "target.view",
  targetManage: "target.manage",
  trainingView: "training.view",
  trainingManage: "training.manage",
  classSchedule: "class.schedule.manage",
  breakdownRequest: "breakdown.request",
  breakdownApprove: "breakdown.approve",
  holidayManage: "holiday.manage",
  traineeView: "trainee.view",
  traineePii: "trainee.view_pii",
  traineeCreate: "trainee.create",
  traineeImport: "trainee.import",
  traineeUpdate: "trainee.update",
  traineeDelete: "trainee.delete",
  rosterFreeze: "roster.freeze",
  duplicateReview: "duplicate.review",
  attendanceView: "attendance.view",
  attendanceSubmit: "attendance.submit",
  correctionRequest: "attendance.correct.request",
  correctionApprove: "attendance.correct.approve",
  outcomeView: "outcome.view",
  outcomeSubmit: "outcome.submit",
  outcomeReview: "outcome.review",
  resubmitApprove: "outcome.resubmit.approve",
  indicatorView: "indicator.view",
  indicatorManage: "indicator.manage",
  kpiRecompute: "kpi.recompute",
  activityView: "activity.view",
  activitySubmit: "activity.submit",
  activityReview: "activity.review",
  activityConfigure: "activity.configure",
  verAggregate: "verification.view_aggregate",
  verSubjects: "verification.view_subjects",
  verPlan: "verification.plan",
  verDraw: "verification.draw",
  verRecord: "verification.record",
  verApprove: "verification.approve",
  verFinalize: "verification.finalize",
  grievanceView: "grievance.view",
  grievanceRespond: "grievance.respond",
  grievanceResolve: "grievance.resolve",
  grievanceManage: "grievance.manage",
  notificationManage: "notification.manage",
  reportView: "report.view",
  reportExport: "report.export",
  datahealthView: "datahealth.view",
} as const;
export type PermKey = (typeof P)[keyof typeof P];

/** Where each role lands after sign-in (relative to /p/$pid). */
export const homePathFor = (r: RoleCode) =>
  isTpRole(r) ? "home" : r === "verification_enumerator" ? "verification/my-work" : "dashboard";
