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 { HubUsersManagement } from '@modules/hub-users-management/hub-users';
import { MerchantUsersManagement } from '@modules/hub-users-management/merchant-users';

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

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: 'hub-users-management',
    canActivate: [authGuard, roleGuard],
    component: HubUsersManagement,
    data: { 
      title: 'Gestion des Utilisateurs',
      module: 'hub-users-management',
      context: 'HUB',
    }
  },

  // ---------------------------
  // Subscriptions
  // ---------------------------
  {
    path: 'subscriptions',
    component: SubscriptionsManagement,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Gestion des Abonnements',
      module: 'subscriptions',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support',
      ]
    }
  },
  {
    path: 'subscriptions/merchant',
    component: SubscriptionsManagement,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Abonnements par Merchant',
      module: 'subscriptions-merchant',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support',
      ]
    }
  },

  // ---------------------------
  // Subscriptions Payments
  // ---------------------------
  {
    path: 'subscriptions/payments',
    component: SubscriptionsManagement,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Paiements des Abonnements',
      module: 'subscriptions-payments',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support',
      ]
    }
  },
  {
    path: 'subscriptions/:id/payments',
    component: SubscriptionPayments,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Détails des Paiements',
      module: 'subscriptions-payments',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support',
      ]
    }
  },

  // ---------------------------
  // Partners (existant - gardé pour référence)
  // ---------------------------
  {
    path: 'merchant-users-management',
    component: MerchantUsersManagement,
    canActivate: [authGuard, roleGuard],
    data: { 
      title: 'Gestion Partners/Marchants',
      module: 'merchant-users-management',
      context: 'MERCHANT',
      requiredRoles: [
        'dcb-admin', 
        'dcb-support', 
        'dcb-partner', 
        'dcb-partner-admin', 
        'dcb-partner-manager', 
        'dcb-partner-support',
      ]
    }
  },
  // ---------------------------
  // 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 {}