<?php

use App\Http\Controllers\Training\TrainingController;
use App\Http\Controllers\Training\PublicController;
use App\Http\Controllers\Training\SessionController;
use App\Http\Controllers\Training\RegistrationController;
use App\Http\Controllers\Training\CertificateController;
use App\Http\Controllers\Training\AttendanceController;
use App\Http\Controllers\Training\ReportController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Training Module Routes
|--------------------------------------------------------------------------
|
| These routes handle the Capacity Building and Training Module functionality.
| Routes are organized by access level: public, authenticated, and admin.
|
*/

// ============================================================================
// PUBLIC ROUTES (No Authentication Required)
// ============================================================================

Route::prefix('training')->name('public.training.')->group(function () {
    // Training Programs Listing
    Route::get('/', [PublicController::class, 'index'])->name('index');

    // Certificate Verification (Public) — must be before /{package} wildcard
    Route::get('/certificates/verify', [PublicController::class, 'verifyCertificatePage'])
        ->name('certificates.verify-page');
    Route::get('/certificates/verify/{code}', [PublicController::class, 'verifyCertificate'])
        ->name('certificates.verify');
    
    // Single Training Program
    Route::get('/{package}', [PublicController::class, 'show'])->name('show');
    
    // Application Form
    Route::get('/{package}/apply', [PublicController::class, 'apply'])->name('apply');
    Route::post('/{package}/apply', [PublicController::class, 'submitApplication'])->name('apply.submit');
    
    // Payment
    Route::get('/{package}/registration/{registration}/payment', [PublicController::class, 'payment'])
        ->name('payment');
    Route::post('/{package}/registration/{registration}/payment', [PublicController::class, 'processPayment'])
        ->name('payment.process');
    
    // Confirmation
    Route::get('/{package}/registration/{registration}/confirmation', [PublicController::class, 'confirmation'])
        ->name('confirmation');
});

// ============================================================================
// AUTHENTICATED ROUTES (Login Required)
// ============================================================================

Route::middleware(['auth', 'verified'])->prefix('dashboard/training')->name('training.')->group(function () {
    
    // ========================================================================
    // Training Packages
    // ========================================================================
    Route::prefix('packages')->name('packages.')->group(function () {
        Route::get('/', [TrainingController::class, 'index'])->name('index')
            ->middleware('permission:view training packages');
        
        Route::get('/create', [TrainingController::class, 'create'])->name('create')
            ->middleware('permission:create training packages');
        
        Route::post('/', [TrainingController::class, 'store'])->name('store')
            ->middleware('permission:create training packages');
        
        Route::get('/{package}', [TrainingController::class, 'show'])->name('show')
            ->middleware('permission:view training packages');
        
        Route::get('/{package}/edit', [TrainingController::class, 'edit'])->name('edit')
            ->middleware('permission:edit training packages');
        
        Route::put('/{package}', [TrainingController::class, 'update'])->name('update')
            ->middleware('permission:edit training packages');
        
        Route::delete('/{package}', [TrainingController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete training packages');
        
        Route::post('/{package}/activate', [TrainingController::class, 'activate'])->name('activate')
            ->middleware('permission:edit training packages');
        
        Route::post('/{package}/deactivate', [TrainingController::class, 'deactivate'])->name('deactivate')
            ->middleware('permission:edit training packages');
    });
    
    // ========================================================================
    // Training Sessions
    // ========================================================================
    Route::prefix('sessions')->name('sessions.')->group(function () {
        Route::get('/', [SessionController::class, 'index'])->name('index')
            ->middleware('permission:view training sessions');
        
        Route::get('/calendar', [SessionController::class, 'calendar'])->name('calendar')
            ->middleware('permission:view training sessions');
        
        Route::get('/calendar/events', [SessionController::class, 'calendarEvents'])->name('calendar.events')
            ->middleware('permission:view training sessions');
        
        Route::get('/create', [SessionController::class, 'create'])->name('create')
            ->middleware('permission:create training sessions');
        
        Route::post('/', [SessionController::class, 'store'])->name('store')
            ->middleware('permission:create training sessions');
        
        Route::get('/{session}', [SessionController::class, 'show'])->name('show')
            ->middleware('permission:view training sessions');
        
        Route::get('/{session}/edit', [SessionController::class, 'edit'])->name('edit')
            ->middleware('permission:edit training sessions');
        
        Route::put('/{session}', [SessionController::class, 'update'])->name('update')
            ->middleware('permission:edit training sessions');
        
        Route::delete('/{session}', [SessionController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete training sessions');
        
        Route::post('/{session}/cancel', [SessionController::class, 'cancel'])->name('cancel')
            ->middleware('permission:edit training sessions');
        
        Route::post('/{session}/postpone', [SessionController::class, 'postpone'])->name('postpone')
            ->middleware('permission:edit training sessions');
        
        Route::post('/{session}/complete', [SessionController::class, 'markCompleted'])->name('complete')
            ->middleware('permission:edit training sessions');
        
        Route::post('/{session}/start', [SessionController::class, 'markStarted'])->name('start')
            ->middleware('permission:edit training sessions');
        
        // Trainer Assignment
        Route::post('/{session}/trainers', [SessionController::class, 'assignTrainer'])->name('trainers.assign')
            ->middleware('permission:edit training sessions');
        
        Route::delete('/{session}/trainers/{trainer}', [SessionController::class, 'removeTrainer'])->name('trainers.remove')
            ->middleware('permission:edit training sessions');
        
        // Attendance
        Route::get('/{session}/attendance', [AttendanceController::class, 'index'])->name('attendance')
            ->middleware('permission:manage training attendance');
        
        Route::post('/{session}/attendance', [AttendanceController::class, 'store'])->name('attendance.store')
            ->middleware('permission:manage training attendance');
        
        Route::post('/{session}/attendance/bulk', [AttendanceController::class, 'bulkStore'])->name('attendance.bulk')
            ->middleware('permission:manage training attendance');
    });
    
    // ========================================================================
    // Registrations
    // ========================================================================
    Route::prefix('registrations')->name('registrations.')->group(function () {
        Route::get('/', [RegistrationController::class, 'index'])->name('index')
            ->middleware('permission:view training registrations');
        
        Route::get('/my-registrations', [RegistrationController::class, 'myRegistrations'])->name('my')
            ->middleware('permission:register for training');
        
        Route::get('/pending', [RegistrationController::class, 'pending'])->name('pending')
            ->middleware('permission:approve training registrations');
        
        Route::get('/create', [RegistrationController::class, 'create'])->name('create')
            ->middleware('permission:register for training');
        
        Route::post('/', [RegistrationController::class, 'store'])->name('store')
            ->middleware('permission:register for training');
        
        Route::get('/{registration}', [RegistrationController::class, 'show'])->name('show')
            ->middleware('permission:view training registrations');
        
        Route::post('/{registration}/approve', [RegistrationController::class, 'approve'])->name('approve')
            ->middleware('permission:approve training registrations');
        
        Route::post('/{registration}/reject', [RegistrationController::class, 'reject'])->name('reject')
            ->middleware('permission:approve training registrations');
        
        Route::post('/{registration}/cancel', [RegistrationController::class, 'cancel'])->name('cancel')
            ->middleware('permission:register for training');
        
        Route::post('/{registration}/complete', [RegistrationController::class, 'complete'])->name('complete')
            ->middleware('permission:edit training registrations');
        
        // Payment
        Route::get('/{registration}/payment', [RegistrationController::class, 'payment'])->name('payment')
            ->middleware('permission:register for training');
        
        Route::post('/{registration}/payment', [RegistrationController::class, 'processPayment'])->name('payment.process')
            ->middleware('permission:register for training');
        
        Route::get('/{registration}/payment/status', [RegistrationController::class, 'paymentStatus'])->name('payment.status')
            ->middleware('permission:register for training');
    });
    
    // ========================================================================
    // Certificates
    // ========================================================================
    Route::prefix('certificates')->name('certificates.')->group(function () {
        Route::get('/', [CertificateController::class, 'index'])->name('index')
            ->middleware('permission:view training certificates');
        
        Route::get('/my-certificates', [CertificateController::class, 'myCertificates'])->name('my')
            ->middleware('permission:view training certificates');
        
        Route::get('/{certificate}', [CertificateController::class, 'show'])->name('show')
            ->middleware('permission:view training certificates');
        
        Route::get('/{certificate}/download', [CertificateController::class, 'download'])->name('download')
            ->middleware('permission:download training certificates');
        
        Route::post('/generate/{registration}', [CertificateController::class, 'generate'])->name('generate')
            ->middleware('permission:generate training certificates');
        
        Route::post('/{certificate}/regenerate', [CertificateController::class, 'regenerate'])->name('regenerate')
            ->middleware('permission:generate training certificates');
        
        Route::post('/{certificate}/revoke', [CertificateController::class, 'revoke'])->name('revoke')
            ->middleware('permission:revoke training certificates');
        
        Route::post('/{certificate}/restore', [CertificateController::class, 'restore'])->name('restore')
            ->middleware('permission:revoke training certificates');
    });
    
    // ========================================================================
    // Reports & Analytics
    // ========================================================================
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index')
            ->middleware('permission:view training reports');
        
        Route::get('/registrations', [ReportController::class, 'registrations'])->name('registrations')
            ->middleware('permission:view training reports');
        
        Route::get('/completion', [ReportController::class, 'completion'])->name('completion')
            ->middleware('permission:view training reports');
        
        Route::get('/revenue', [ReportController::class, 'revenue'])->name('revenue')
            ->middleware('permission:view training reports');
        
        Route::get('/export/{type}', [ReportController::class, 'export'])->name('export')
            ->middleware('permission:export training reports');
    });
});

// ============================================================================
// API ROUTES (For AJAX Requests)
// ============================================================================

Route::middleware(['auth'])->prefix('api/training')->name('api.training.')->group(function () {
    // Calendar Events
    Route::get('/calendar/events', [SessionController::class, 'apiCalendarEvents'])->name('calendar.events');
    
    // Package Statistics
    Route::get('/packages/{package}/stats', [TrainingController::class, 'apiStats'])->name('packages.stats');
    
    // Payment Status Check
    Route::get('/registrations/{registration}/payment/status', [RegistrationController::class, 'apiPaymentStatus'])->name('registrations.payment.status');
    
    // Attendance Quick Actions
    Route::post('/attendance/{attendance}/check-in', [AttendanceController::class, 'apiCheckIn'])->name('attendance.check-in');
    Route::post('/attendance/{attendance}/check-out', [AttendanceController::class, 'apiCheckOut'])->name('attendance.check-out');
    
    // Certificate Verification
    Route::post('/certificates/verify', [CertificateController::class, 'apiVerify'])->name('certificates.verify');
});
