/**
 * Sistema de permissões baseado em roles
 */

export type Permission = 
  | 'admin.users.view'
  | 'admin.users.create'
  | 'admin.users.edit'
  | 'admin.users.delete'
  | 'admin.roles.view'
  | 'admin.roles.create'
  | 'admin.roles.edit'
  | 'admin.roles.delete'
  | 'admin.audit.view'
  | 'rh.agents.view'
  | 'rh.agents.create'
  | 'rh.agents.edit'
  | 'rh.agents.delete'
  | 'rh.payroll.view'
  | 'rh.payroll.create'
  | 'rh.payroll.edit'
  | 'org.ministries.view'
  | 'org.ministries.create'
  | 'org.ministries.edit'
  | 'org.ministries.delete'
  | 'budget.view'
  | 'budget.create'
  | 'budget.edit'
  | 'treasury.view'
  | 'treasury.create'
  | 'treasury.edit';

/**
 * Mapeamento de roles para permissões
 */
const ROLE_PERMISSIONS: Record<string, Permission[]> = {
  ADMIN: [
    // Admin
    'admin.users.view',
    'admin.users.create',
    'admin.users.edit',
    'admin.users.delete',
    'admin.roles.view',
    'admin.roles.create',
    'admin.roles.edit',
    'admin.roles.delete',
    'admin.audit.view',
    // RH
    'rh.agents.view',
    'rh.agents.create',
    'rh.agents.edit',
    'rh.agents.delete',
    'rh.payroll.view',
    'rh.payroll.create',
    'rh.payroll.edit',
    // Org
    'org.ministries.view',
    'org.ministries.create',
    'org.ministries.edit',
    'org.ministries.delete',
    // Budget
    'budget.view',
    'budget.create',
    'budget.edit',
    // Treasury
    'treasury.view',
    'treasury.create',
    'treasury.edit',
  ],
  RH_MANAGER: [
    'rh.agents.view',
    'rh.agents.create',
    'rh.agents.edit',
    'rh.payroll.view',
    'rh.payroll.create',
    'rh.payroll.edit',
  ],
  RH_USER: [
    'rh.agents.view',
    'rh.payroll.view',
  ],
  ORG_MANAGER: [
    'org.ministries.view',
    'org.ministries.create',
    'org.ministries.edit',
    'org.ministries.delete',
  ],
  BUDGET_MANAGER: [
    'budget.view',
    'budget.create',
    'budget.edit',
  ],
  TREASURY_MANAGER: [
    'treasury.view',
    'treasury.create',
    'treasury.edit',
  ],
};

/**
 * Verifica se um usuário tem uma permissão específica
 */
export function hasPermission(userRoles: string[], permission: Permission): boolean {
  return userRoles.some((role) => {
    const rolePermissions = ROLE_PERMISSIONS[role] || [];
    return rolePermissions.includes(permission);
  });
}

/**
 * Verifica se um usuário tem qualquer uma das permissões
 */
export function hasAnyPermission(
  userRoles: string[],
  permissions: Permission[]
): boolean {
  return permissions.some((permission) => hasPermission(userRoles, permission));
}

/**
 * Verifica se um usuário tem todas as permissões
 */
export function hasAllPermissions(
  userRoles: string[],
  permissions: Permission[]
): boolean {
  return permissions.every((permission) => hasPermission(userRoles, permission));
}

/**
 * Obtém todas as permissões de um usuário
 */
export function getUserPermissions(userRoles: string[]): Permission[] {
  const allPermissions = new Set<Permission>();
  
  userRoles.forEach((role) => {
    const rolePermissions = ROLE_PERMISSIONS[role] || [];
    rolePermissions.forEach((permission) => allPermissions.add(permission));
  });
  
  return Array.from(allPermissions);
}

