import { Injectable } from '@angular/core';

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

@Injectable({ providedIn: 'root' })
export class PermissionsService {
  private readonly permissions: ModulePermission[] = [
    // Dashboard
    {
      module: 'dcb-dashboard',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ],
    },

    // Transactions
    {
      module: 'transactions',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ],
    },

    // Merchants/Partners
    {
      module: 'merchant-partners',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'],
    },

    // Operators (Admin only)
    {
      module: 'operators',
      roles: ['dcb-admin'],
      children: {
        'config': ['dcb-admin'],
        'stats': ['dcb-admin']
      }
    },

    // Webhooks
    {
      module: 'webhooks',
      roles: ['dcb-admin', 'dcb-partner'],
      children: {
        'history': ['dcb-admin', 'dcb-partner'],
        'status': ['dcb-admin', 'dcb-partner'],
        'retry': ['dcb-admin']
      }
    },

    // Users (Admin only)
    {
      module: 'users',
      roles: ['dcb-admin', 'dcb-support']
    },

    // Support (All authenticated users)
    {
      module: 'settings',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    },

    // Integrations (Admin only)
    {
      module: 'integrations',
      roles: ['dcb-admin']
    },

    // Support (All authenticated users)
    {
      module: 'support',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    },

    // Profile (All authenticated users)
    {
      module: 'profile',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    },

    // Documentation (All authenticated users)
    {
      module: 'documentation',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    },

    // Help (All authenticated users)
    {
      module: 'help',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    },

    // About (All authenticated users)
    {
      module: 'about',
      roles: [
        'dcb-admin', 
        'dcb-partner', 
        'dcb-support', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support'
      ]
    }
  ];

  canAccessModule(modulePath: string, userRoles: string[]): boolean {
    if (!userRoles || userRoles.length === 0) {
      return false;
    }

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

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

    // Check main module access
    const hasModuleAccess = this.hasAnyRole(permission.roles, userRoles);
    if (!hasModuleAccess) return false;

    // Check sub-module access if specified
    if (subModule && permission.children) {
      const subModuleRoles = permission.children[subModule];
      if (!subModuleRoles) {
        console.warn(`No permission configuration for submodule: ${mainModule}/${subModule}`);
        return false;
      }
      return this.hasAnyRole(subModuleRoles, 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(role => userRoles.includes(role));
  }

  getAccessibleModules(userRoles: string[]): string[] {
    return this.permissions
      .filter(permission => this.hasAnyRole(permission.roles, userRoles))
      .map(permission => permission.module);
  }
}