import { Injectable } from '@angular/core';
import { UserRole } from '@core/models/dcb-bo-hub-user.model';

export interface ModulePermission {
  module: string;
  roles: string[];
  children?: { [key: string]: string[] };
}

@Injectable({ providedIn: 'root' })
export class PermissionsService {
  private readonly permissions: ModulePermission[] = [
    // Dashboard - Tout le monde
    {
      module: 'dcb-dashboard',
      roles: this.allRoles,
    },
    {
      module: 'auth',
      roles: this.allRoles,
    },
    {
      module: 'transactions',
      roles: this.allRoles,
    },
    // Users Admin et Support
    {
      module: 'hub-users-management',
      roles: [UserRole.DCB_ADMIN, UserRole.DCB_SUPPORT],
    },
    // Merchant Users
    {
      module: 'merchant-users-management',
      roles: this.allRoles,
    },
    // Operators - Admin seulement
    {
      module: 'operators',
      roles: [UserRole.DCB_ADMIN],
      children: {
        'config': [UserRole.DCB_ADMIN],
        'stats': [UserRole.DCB_ADMIN]
      }
    },
    // Webhooks - Admin et Partner
    {
      module: 'webhooks',
      roles: [UserRole.DCB_ADMIN, UserRole.DCB_PARTNER],
      children: {
        'history': [UserRole.DCB_ADMIN, UserRole.DCB_PARTNER],
        'status': [UserRole.DCB_ADMIN, UserRole.DCB_PARTNER],
        'retry': [UserRole.DCB_ADMIN]
      }
    },
    // Settings - Tout le monde
    {
      module: 'settings',
      roles: this.allRoles
    },

    // Subscriptions 
    {
      module: 'subscriptions',
      roles: this.allRoles
    },
    {
      module: 'subscriptions-merchant', 
      roles: this.allRoles
    },

    // Payments
    {
      module: 'subscriptions-payments',
      roles: this.allRoles
    },
    
    // Settings - Tout le monde
    {
      module: 'merchant-configs',
      roles: this.allRoles
    },

    // Integrations - Admin seulement
    {
      module: 'integrations',
      roles: [UserRole.DCB_ADMIN]
    },
    // Modules publics - Tout le monde
    {
      module: 'support',
      roles: this.allRoles
    },
    {
      module: 'profile',
      roles: this.allRoles
    },
    {
      module: 'documentation',
      roles: this.allRoles
    },
    {
      module: 'help',
      roles: this.allRoles
    },
    {
      module: 'about',
      roles: this.allRoles
    }
  ];

  // Tous les rôles DCB
  private get allRoles(): string[] {
    return Object.values(UserRole);
  }

  canAccessModule(modulePath: string, userRoles: string[]): boolean {
    if (!userRoles?.length) return false;

    const [mainModule, subModule] = modulePath.split('/');
    const permission = this.findPermission(mainModule);

    if (!permission) {
      console.warn(`No permission for module: ${mainModule}`);
      return false;
    }

    // Vérifier accès module principal
    const hasMainAccess = this.hasAnyRole(permission.roles, userRoles);
    if (!hasMainAccess) return false;

    // Vérifier sous-module si nécessaire
    if (subModule && permission.children) {
      const subRoles = permission.children[subModule];
      if (!subRoles) return false;
      return this.hasAnyRole(subRoles, userRoles);
    }

    return true;
  }

  private findPermission(module: string): ModulePermission | undefined {
    return this.permissions.find(p => p.module === module);
  }

  private hasAnyRole(requiredRoles: string[], userRoles: string[]): boolean {
    return requiredRoles.some(required => 
      userRoles.some(user => user.toLowerCase() === required.toLowerCase())
    );
  }
}