// SHiRE LimbForge — permission groups, presets and role definitions.
// Builder permissions are individually toggleable; presets provide defaults.

export const ROLES = [
  { key: 'owner_admin', label: 'Owner Admin' },
  { key: 'admin', label: 'Admin' },
  { key: 'builder', label: 'Builder / Designer' },
  { key: 'customer', label: 'Customer' },
  { key: 'reviewer', label: 'Future Reviewer (read-only)' },
];

export const ACCOUNT_STATUSES = [
  'Invited', 'Invitation Expired', 'Active', 'Email Verification Required',
  'Password Reset Required', 'Suspended', 'Archived', 'Access Revoked',
];

export const PERMISSION_GROUPS = [
  { group: 'Customer records', permissions: [
    { key: 'customers.view_assigned', label: 'View assigned customers' },
    { key: 'customers.view_all', label: 'View all customers' },
    { key: 'customers.edit_details', label: 'Edit customer details' },
    { key: 'customers.edit_notes', label: 'Edit private notes' },
    { key: 'customers.access_funding', label: 'Access funding information' },
    { key: 'customers.archive', label: 'Archive customers' },
  ]},
  { group: 'Measurements', permissions: [
    { key: 'measurements.record', label: 'Record measurements' },
    { key: 'measurements.edit', label: 'Edit entered measurements' },
    { key: 'measurements.verify', label: 'Verify measurements' },
    { key: 'measurements.supersede', label: 'Supersede measurements' },
    { key: 'measurements.export', label: 'Export measurements' },
  ]},
  { group: 'Components', permissions: [
    { key: 'components.view', label: 'View components' },
    { key: 'components.edit', label: 'Edit components' },
    { key: 'components.verify', label: 'Verify component dimensions' },
    { key: 'components.manage_sources', label: 'Manage manufacturer sources' },
    { key: 'components.manage_ossiguard', label: 'Manage Ossiguard profiles' },
  ]},
  { group: 'Design', permissions: [
    { key: 'design.use_forge', label: 'Use Design Forge' },
    { key: 'design.edit', label: 'Edit custom designs' },
    { key: 'design.approve_concepts', label: 'Approve concepts' },
    { key: 'design.change_branding', label: 'Change SHiRE branding' },
    { key: 'design.approve_for_quote', label: 'Approve design for quote' },
  ]},
  { group: 'Financial', permissions: [
    { key: 'finance.view_quotes', label: 'View quotes' },
    { key: 'finance.edit_quotes', label: 'Edit quotes' },
    { key: 'finance.view_orders', label: 'View orders' },
    { key: 'finance.record_payment', label: 'Record payment' },
    { key: 'finance.record_approval', label: 'Record NDIS or plan-manager approval' },
  ]},
  { group: 'Manufacturing', permissions: [
    { key: 'mfg.create_jobs', label: 'Create jobs' },
    { key: 'mfg.assign_printers', label: 'Assign printers' },
    { key: 'mfg.update_status', label: 'Update production status' },
    { key: 'mfg.record_inspection', label: 'Record inspection' },
    { key: 'mfg.complete_covers', label: 'Complete covers' },
  ]},
  { group: 'Administration', permissions: [
    { key: 'admin.manage_users', label: 'Manage users' },
    { key: 'admin.invite_builders', label: 'Invite Builders' },
    { key: 'admin.invite_admins', label: 'Invite Admins' },
    { key: 'admin.manage_permissions', label: 'Manage permissions' },
    { key: 'admin.view_audit', label: 'View audit log' },
    { key: 'admin.export_data', label: 'Export system data' },
    { key: 'admin.configure_migration', label: 'Configure migration' },
  ]},
];

export const ALL_PERMISSIONS = PERMISSION_GROUPS.flatMap(g => g.permissions.map(p => p.key));

const ALL = ALL_PERMISSIONS;
const builderBase = [
  'customers.view_assigned', 'customers.edit_notes',
  'measurements.record', 'measurements.edit',
  'components.view',
  'design.use_forge',
];

export const PERMISSION_PRESETS = {
  owner_admin: { label: 'Owner Admin', role: 'owner_admin', permissions: ALL },
  workshop_admin: {
    label: 'Workshop Admin', role: 'admin',
    permissions: ALL.filter(p => p !== 'admin.invite_admins' && p !== 'admin.configure_migration'),
  },
  senior_builder: {
    label: 'Senior Builder', role: 'builder',
    permissions: [...builderBase, 'measurements.verify', 'measurements.supersede',
      'components.edit', 'components.verify', 'components.manage_ossiguard',
      'design.edit', 'design.approve_concepts', 'design.approve_for_quote',
      'finance.view_quotes', 'finance.view_orders',
      'mfg.create_jobs', 'mfg.update_status', 'mfg.record_inspection', 'mfg.complete_covers'],
  },
  builder: {
    label: 'Builder', role: 'builder',
    permissions: [...builderBase, 'design.edit', 'mfg.update_status', 'mfg.record_inspection'],
  },
  design_only: { label: 'Design Only', role: 'builder', permissions: ['customers.view_assigned', 'design.use_forge', 'design.edit'] },
  measurement_only: { label: 'Measurement Only', role: 'builder', permissions: ['customers.view_assigned', 'components.view', 'measurements.record', 'measurements.edit', 'measurements.verify'] },
  manufacturing_only: { label: 'Manufacturing Only', role: 'builder', permissions: ['mfg.update_status', 'mfg.record_inspection', 'mfg.complete_covers'] },
  customer: { label: 'Customer', role: 'customer', permissions: [] },
};

export const BUILDER_SKILLS = [
  'Customer Review', 'Measurements', 'Prosthetic Components', 'Babylon 3D Mapping',
  'Design Forge', 'Ossiguard', 'CAD Preparation', 'Printing', 'Post-processing',
  'Inspection', 'Shipping',
];

export const roleHomePath = (role) => ({
  owner_admin: '/admin', admin: '/admin', builder: '/builder', customer: '/portal', reviewer: '/admin',
}[role] || '/portal');

export const resolvePermissions = (account) => {
  if (!account) return [];
  if (account.role === 'owner_admin') return ALL;
  if (account.role === 'customer') return [];
  const preset = PERMISSION_PRESETS[account.permission_preset];
  const base = preset ? preset.permissions : [];
  const custom = account.custom_permissions || {};
  const toggledOn = Object.keys(custom).filter(k => custom[k] === true);
  const toggledOff = Object.keys(custom).filter(k => custom[k] === false);
  return [...new Set([...base.filter(p => !toggledOff.includes(p)), ...toggledOn])];
};

export const hasPermission = (account, perm) => {
  if (!account) return false;
  if (account.role === 'owner_admin') return true;
  return resolvePermissions(account).includes(perm);
};