import { useAuth, type AuthUser } from "~/context/AuthContext"; export type OperationsRole = "business" | "model" | "admin"; export type OperationsPageKey = | "workbench" | "usage" | "models" | "banks" | "deployed-models" | "monitoring" | "monitoring-detail" | "reports" | "report-summary" | "workflows" | "knowledge" | "rules" | "prompts" | "settings"; export type OperationsAction = | "workflow:create" | "workflow:feedback" | "workflow:submit-material" | "workflow:advance" | "workflow:online" | "workflow:business-confirm" | "monitor:initial-review" | "monitor:final-review" | "report:edit" | "report:export" | "report:send" | "rules:publish" | "rules:rollback" | "rules:simulate" | "rules:manage" | "prompts:manage" | "prompts:regression" | "settings:manage"; const PAGE_ROLE_VISIBILITY: Record = { workbench: ["business", "model", "admin"], usage: ["admin"], models: ["business", "model", "admin"], banks: ["business", "model", "admin"], "deployed-models": ["model", "admin"], monitoring: ["business", "model", "admin"], "monitoring-detail": ["business", "model", "admin"], reports: ["business", "model", "admin"], "report-summary": ["business", "model", "admin"], workflows: ["business", "model", "admin"], knowledge: ["model", "admin"], rules: ["model", "admin"], prompts: ["model", "admin"], settings: ["admin"], }; const ACTION_ROLE_VISIBILITY: Record = { "workflow:create": ["business", "admin"], "workflow:feedback": ["business", "admin"], "workflow:submit-material": ["model", "admin"], "workflow:advance": ["model", "admin"], "workflow:online": ["model", "admin"], "workflow:business-confirm": ["business", "admin"], "monitor:initial-review": ["model", "admin"], "monitor:final-review": ["business", "admin"], "report:edit": ["model", "admin"], "report:export": ["business", "model", "admin"], "report:send": ["model", "admin"], "rules:publish": ["admin"], "rules:rollback": ["admin"], "rules:simulate": ["admin"], "rules:manage": ["admin"], "prompts:manage": ["model", "admin"], "prompts:regression": ["model", "admin"], "settings:manage": ["admin"], }; export const OPERATIONS_PAGE_PERMISSIONS: Record = { workbench: "operations:workbench:view", usage: "operations:usage:view", models: "operations:model-overview:view", banks: "operations:bank-overview:view", "deployed-models": "operations:deployed-models:view", monitoring: "operations:monitoring-overview:view", "monitoring-detail": "operations:monitoring-detail:view", reports: "operations:report:view", "report-summary": "operations:report-summary:view", workflows: "operations:workflow:view", knowledge: "operations:knowledge:view", rules: "operations:rules:view", prompts: "operations:prompt:view", settings: "operations:settings:view", }; export const OPERATIONS_ACTION_PERMISSIONS: Record = { "workflow:create": "operations:workflow:create", "workflow:feedback": "operations:workflow:feedback", "workflow:submit-material": "operations:workflow:submit-material", "workflow:advance": "operations:workflow:advance", "workflow:online": "operations:workflow:online", "workflow:business-confirm": "operations:workflow:confirm", "monitor:initial-review": "operations:monitoring-detail:model-review", "monitor:final-review": "operations:monitoring-detail:business-review", "report:edit": "operations:report:edit", "report:export": "operations:report:export", "report:send": "operations:report:send", "rules:publish": "operations:rules:publish", "rules:rollback": "operations:rules:rollback", "rules:simulate": "operations:rules:simulate", "rules:manage": "operations:rules:publish", "prompts:manage": "operations:prompt:edit", "prompts:regression": "operations:prompt:regression", "settings:manage": "operations:settings:view", }; export function resolveOperationsRole(user: AuthUser | null): OperationsRole { if (user?.is_system_admin || user?.role_code === "admin") return "admin"; const code = user?.role_code?.toLowerCase() ?? ""; if (["business", "business_team", "biz"].includes(code)) return "business"; return "model"; } export function useOperationsRole(): OperationsRole { return resolveOperationsRole(useAuth().user); } export function isOperationsPageVisibleForRole(role: OperationsRole, page: OperationsPageKey): boolean { return PAGE_ROLE_VISIBILITY[page].includes(role); } export function isOperationsActionVisibleForRole(role: OperationsRole, action: OperationsAction): boolean { return ACTION_ROLE_VISIBILITY[action].includes(role); } export function useOperationsPermission(permissionCode: string): boolean { const { user } = useAuth(); return Boolean(user?.is_system_admin || user?.permissions.includes(permissionCode)); } export function useCanOperationsPage(page: OperationsPageKey): boolean { return useOperationsPermission(OPERATIONS_PAGE_PERMISSIONS[page]); } export function useCanOperationsAction(action: OperationsAction): boolean { return useOperationsPermission(OPERATIONS_ACTION_PERMISSIONS[action]); } export function operationsPagePermission(page: OperationsPageKey): string { return OPERATIONS_PAGE_PERMISSIONS[page]; } export function operationsPageFromPath(pathname: string): OperationsPageKey { const normalized = pathname.replace(/^\/operations\/?/, ""); if (normalized.startsWith("monitoring/")) return "monitoring-detail"; const path = normalized.split("/")[0] ?? ""; if (!path) return "workbench"; if (path === "monitoring") return "monitoring"; if (path in PAGE_ROLE_VISIBILITY) return path as OperationsPageKey; return "workbench"; }