import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { authGuard } from '../core/guards/auth.guard';
import { roleGuard } from '../core/guards/role.guard';
import { Users } from '@modules/users/users';

// Composants principaux
import { DcbDashboard } from './dcb-dashboard/dcb-dashboard';
import { Team } from './team/team';
import { Transactions } from './transactions/transactions';
import { MerchantPartners } from './merchant-partners/merchant-partners';
import { OperatorsConfig } from './operators/config/config';
import { OperatorsStats } from './operators/stats/stats';
import { WebhooksHistory } from './webhooks/history/history';
import { WebhooksStatus } from './webhooks/status/status';
import { WebhooksRetry } from './webhooks/retry/retry';
import { Settings } from './settings/settings';
import { Integrations } from './integrations/integrations';
import { Support } from './support/support';
import { MyProfile } from './profile/profile';
import { Documentation } from './documentation/documentation';
import { Help } from './help/help';
import { About } from './about/about';

const routes: Routes = [
  // ---------------------------
  // Dashboard
  // ---------------------------
  {
    path: 'dcb-dashboard',
    canActivate: [authGuard, roleGuard], 
    component: DcbDashboard,
    data: { 
      title: 'Dashboard DCB',
      module: 'dcb-dashboard'
    }
  },

  // ---------------------------
  // Team
  // ---------------------------
  {
    path: 'team',
    component: Team,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Team',
      module: 'team'
    }
  },

  // ---------------------------
  // Transactions
  // ---------------------------
  {
    path: 'transactions',
    component: Transactions,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Transactions DCB',
      module: 'transactions'
    }
  },
  {
    path: 'transactions/:id',
    component: Transactions,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Détails Transaction',
      module: 'transactions'
    }
  },
  
  // ---------------------------
  // Users (Admin seulement)
  // ---------------------------
  {
    path: 'users',
    canActivate: [authGuard, roleGuard],
    component: Users,
    data: { 
      title: 'Gestion des Utilisateurs',
      module: 'users'
    }
  },

  // ---------------------------
  // Partners
  // ---------------------------
  {
    path: 'merchant-partners',
    component: MerchantPartners,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Gestion Partners/Marchants',
      module: 'merchant-partners',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-suport',
      ]
    }
  },
  
  // ---------------------------
  // Operators (Admin seulement)
  // ---------------------------
  {
    path: 'operators',
    canActivate: [authGuard, roleGuard],
    data: { module: 'operators' },
    children: [
      { 
        path: 'config', 
        component: OperatorsConfig, 
        data: { 
          title: 'Paramètres d\'Intégration',
          module: 'operators/config'
        } 
      },
      { 
        path: 'stats', 
        component: OperatorsStats, 
        data: { 
          title: 'Performance & Monitoring',
          module: 'operators/stats'
        } 
      },
    ]
  },

  // ---------------------------
  // Webhooks
  // ---------------------------
  {
    path: 'webhooks',
    canActivate: [authGuard, roleGuard],
    data: { module: 'webhooks' },
    children: [
      { 
        path: 'history', 
        component: WebhooksHistory, 
        data: { 
          title: 'Historique',
          module: 'webhooks/history'
        } 
      },
      { 
        path: 'status', 
        component: WebhooksStatus, 
        data: { 
          title: 'Statut des Requêtes',
          module: 'webhooks/status'
        } 
      },
      { 
        path: 'retry', 
        component: WebhooksRetry, 
        data: { 
          title: 'Relancer Webhook',
          module: 'webhooks/retry'
        } 
      },
    ]
  },

  // ---------------------------
  // Settings
  // ---------------------------
  { 
    path: 'settings', 
    component: Settings, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'Paramètres Système',
      module: 'settings'
    } 
  },

  // ---------------------------
  // Integrations (Admin seulement)
  // ---------------------------
  { 
    path: 'integrations', 
    component: Integrations, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'Intégrations Externes',
      module: 'integrations'
    } 
  },

  // ---------------------------
  // Support & Profile (Tous les utilisateurs authentifiés)
  // ---------------------------
  { 
    path: 'dcb-support', 
    component: Support, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'Support',
      module: 'dcb-support'
    } 
  },
  {
    path: 'profile',
    component: MyProfile,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Mon Profil',
      module: 'profile'
    }
  },

  // ---------------------------
  // Documentation & Help (Tous les utilisateurs authentifiés)
  // ---------------------------
  { 
    path: 'documentation', 
    component: Documentation, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'Documentation',
      module: 'documentation'
    } 
  },
  { 
    path: 'help', 
    component: Help, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'Aide',
      module: 'help'
    } 
  },
  { 
    path: 'about', 
    component: About, 
    canActivate: [authGuard, roleGuard], 
    data: { 
      title: 'À propos',
      module: 'about'
    } 
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class ModulesRoutes {
  constructor() {
    console.log('Modules routes loaded:', routes);
  }
}