<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Mentorship\CategoryController;
use App\Http\Controllers\Mentorship\ProgrammeController;
use App\Http\Controllers\Mentorship\ApplicationController;
use App\Http\Controllers\Mentorship\AssignmentController;
use App\Http\Controllers\Mentorship\SessionController;
use App\Http\Controllers\Mentorship\CertificateController;
use App\Http\Controllers\Mentorship\PaymentController;
use App\Http\Controllers\Mentorship\PublicController;

/*
|--------------------------------------------------------------------------
| Mentorship Module Web Routes
|--------------------------------------------------------------------------
|
| These routes are for the Mentorship Management Module. Routes are organized
| by functionality and follow controller-based routing patterns.
|
*/

/*
|--------------------------------------------------------------------------
| Public Routes (No Authentication Required)
|--------------------------------------------------------------------------
*/
Route::prefix('mentorship')->name('mentorship.public.')->group(function () {
    // Main public page with programme listings
    Route::get('/', [PublicController::class, 'index'])->name('index');
    
    // Programme detail page
    Route::get('/programme/{programme}', [PublicController::class, 'programme'])->name('programme');
    
    // Category page
    Route::get('/category/{category}', [PublicController::class, 'category'])->name('category');
    
    // Application form
    Route::get('/apply', [PublicController::class, 'apply'])->name('apply');
    Route::post('/apply', [PublicController::class, 'submitApplication'])->name('apply.store');
    
    // Application status tracking
    Route::get('/status/{application}', [PublicController::class, 'status'])->name('status');
    Route::get('/check-status', [PublicController::class, 'checkStatusForm'])->name('check-status');
    Route::post('/check-status', [PublicController::class, 'checkStatus'])->name('check-status.submit');
    Route::get('/verify-status', [PublicController::class, 'checkStatusForm'])->name('verify-status');
    Route::post('/verify-status/check', [PublicController::class, 'checkStatus'])->name('verify-status.check');
    
    // AJAX endpoints for public pages
    Route::get('/category/{category}/programmes', [PublicController::class, 'getByCategory'])->name('by-category');
    Route::get('/search', [PublicController::class, 'search'])->name('search');
    Route::get('/programme/{programme}/details', [PublicController::class, 'getProgrammeDetails'])->name('programme-details');
    Route::get('/categories', [PublicController::class, 'getCategories'])->name('categories');
});

// Certificate verification (public)
Route::prefix('mentorship/certificates')->name('mentorship.certificates.')->group(function () {
    Route::get('/verify', [CertificateController::class, 'verifyPage'])->name('verify-page');
    Route::get('/verify/{code}', [CertificateController::class, 'verify'])->name('verify');
});

/*
|--------------------------------------------------------------------------
| Payment Routes (Mixed Authentication)
|--------------------------------------------------------------------------
*/
Route::prefix('mentorship/payments')->name('mentorship.payments.')->group(function () {
    // Payment callback (no auth - from MTN MoMo)
    Route::post('/callback', [PaymentController::class, 'callback'])->name('callback');
    
    // Payment initiation (can be guest for public applications)
    Route::get('/create', [PaymentController::class, 'create'])->name('create');
    Route::post('/create', [PaymentController::class, 'store'])->name('store');
    
    // Payment status (can be guest with session verification)
    Route::get('/{payment}/status', [PaymentController::class, 'status'])->name('status');
    Route::get('/{payment}/check', [PaymentController::class, 'checkStatus'])->name('check');
});

/*
|--------------------------------------------------------------------------
| Authenticated Routes
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'verified'])->group(function () {
    
    /*
    |----------------------------------------------------------------------
    | Categories Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/categories')->name('mentorship.categories.')->group(function () {
        Route::get('/', [CategoryController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship categories');
        Route::get('/create', [CategoryController::class, 'create'])->name('create')
            ->middleware('permission:create mentorship categories');
        Route::post('/', [CategoryController::class, 'store'])->name('store')
            ->middleware('permission:create mentorship categories');
        Route::get('/{category}', [CategoryController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship categories');
        Route::get('/{category}/edit', [CategoryController::class, 'edit'])->name('edit')
            ->middleware('permission:edit mentorship categories');
        Route::put('/{category}', [CategoryController::class, 'update'])->name('update')
            ->middleware('permission:edit mentorship categories');
        Route::delete('/{category}', [CategoryController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete mentorship categories');
        
        // AJAX actions
        Route::post('/{category}/activate', [CategoryController::class, 'activate'])->name('activate')
            ->middleware('permission:edit mentorship categories');
        Route::post('/{category}/deactivate', [CategoryController::class, 'deactivate'])->name('deactivate')
            ->middleware('permission:edit mentorship categories');
        Route::post('/{category}/move', [CategoryController::class, 'move'])->name('move')
            ->middleware('permission:edit mentorship categories');
        Route::post('/sort-order', [CategoryController::class, 'updateSortOrder'])->name('sort-order')
            ->middleware('permission:edit mentorship categories');
        
        // AJAX data endpoints
        Route::get('/tree/all', [CategoryController::class, 'getTree'])->name('tree');
        Route::get('/select/list', [CategoryController::class, 'getForSelect'])->name('select');
    });
    
    /*
    |----------------------------------------------------------------------
    | Programmes Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/programmes')->name('mentorship.programmes.')->group(function () {
        Route::get('/', [ProgrammeController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship programmes');
        Route::get('/create', [ProgrammeController::class, 'create'])->name('create')
            ->middleware('permission:create mentorship programmes');
        Route::post('/', [ProgrammeController::class, 'store'])->name('store')
            ->middleware('permission:create mentorship programmes');
        Route::get('/{programme}', [ProgrammeController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship programmes');
        Route::get('/{programme}/edit', [ProgrammeController::class, 'edit'])->name('edit')
            ->middleware('permission:edit mentorship programmes');
        Route::put('/{programme}', [ProgrammeController::class, 'update'])->name('update')
            ->middleware('permission:edit mentorship programmes');
        Route::delete('/{programme}', [ProgrammeController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete mentorship programmes');
        
        // AJAX actions
        Route::post('/{programme}/activate', [ProgrammeController::class, 'activate'])->name('activate')
            ->middleware('permission:edit mentorship programmes');
        Route::post('/{programme}/deactivate', [ProgrammeController::class, 'deactivate'])->name('deactivate')
            ->middleware('permission:edit mentorship programmes');
        Route::post('/{programme}/feature', [ProgrammeController::class, 'feature'])->name('feature')
            ->middleware('permission:edit mentorship programmes');
        Route::post('/{programme}/unfeature', [ProgrammeController::class, 'unfeature'])->name('unfeature')
            ->middleware('permission:edit mentorship programmes');
        Route::get('/{programme}/statistics', [ProgrammeController::class, 'statistics'])->name('statistics')
            ->middleware('permission:view mentorship programmes');
        Route::post('/{programme}/duplicate', [ProgrammeController::class, 'duplicate'])->name('duplicate')
            ->middleware('permission:create mentorship programmes');
        
        // AJAX data endpoints
        Route::get('/select/list', [ProgrammeController::class, 'getForSelect'])->name('select');
    });
    
    /*
    |----------------------------------------------------------------------
    | Applications Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/applications')->name('mentorship.applications.')->group(function () {
        Route::get('/', [ApplicationController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship applications');
        Route::get('/{application}', [ApplicationController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship applications');
        Route::get('/{application}/review', [ApplicationController::class, 'edit'])->name('review')
            ->middleware('permission:review mentorship applications');
        
        // Actions
        Route::post('/{application}/under-review', [ApplicationController::class, 'markUnderReview'])->name('under-review')
            ->middleware('permission:review mentorship applications');
        Route::post('/{application}/approve', [ApplicationController::class, 'approve'])->name('approve')
            ->middleware('permission:approve mentorship applications');
        Route::post('/{application}/reject', [ApplicationController::class, 'reject'])->name('reject')
            ->middleware('permission:reject mentorship applications');
        
        // Bulk actions
        Route::post('/bulk-approve', [ApplicationController::class, 'bulkApprove'])->name('bulk-approve')
            ->middleware('permission:approve mentorship applications');
        
        // Export
        Route::get('/export', [ApplicationController::class, 'export'])->name('export')
            ->middleware('permission:view mentorship applications');
        
        // AJAX data endpoints
        Route::get('/pending-count', [ApplicationController::class, 'pendingCount'])->name('pending-count');
    });
    
    /*
    |----------------------------------------------------------------------
    | Assignments Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/assignments')->name('mentorship.assignments.')->group(function () {
        Route::get('/', [AssignmentController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship assignments');
        Route::get('/create', [AssignmentController::class, 'create'])->name('create')
            ->middleware('permission:create mentorship assignments');
        Route::post('/', [AssignmentController::class, 'store'])->name('store')
            ->middleware('permission:create mentorship assignments');
        Route::get('/{assignment}', [AssignmentController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship assignments');
        Route::get('/{assignment}/edit', [AssignmentController::class, 'edit'])->name('edit')
            ->middleware('permission:edit mentorship assignments');
        Route::put('/{assignment}', [AssignmentController::class, 'update'])->name('update')
            ->middleware('permission:edit mentorship assignments');
        
        // Actions
        Route::post('/{assignment}/accept', [AssignmentController::class, 'accept'])->name('accept')
            ->middleware('permission:edit mentorship assignments');
        Route::post('/{assignment}/complete', [AssignmentController::class, 'complete'])->name('complete')
            ->middleware('permission:complete mentorship assignments');
        Route::post('/{assignment}/cancel', [AssignmentController::class, 'cancel'])->name('cancel')
            ->middleware('permission:cancel mentorship assignments');
        Route::post('/{assignment}/reassign', [AssignmentController::class, 'reassign'])->name('reassign')
            ->middleware('permission:reassign mentorship assignments');
        
        // Auto-matching
        Route::post('/auto-match/{programme}', [AssignmentController::class, 'autoMatch'])->name('auto-match')
            ->middleware('permission:create mentorship assignments');
        
        // AJAX data endpoints
        Route::get('/mentors/{programme}', [AssignmentController::class, 'getAvailableMentors'])->name('available-mentors');
        Route::get('/mentees/{programme}', [AssignmentController::class, 'getUnassignedMentees'])->name('unassigned-mentees');
    });
    
    /*
    |----------------------------------------------------------------------
    | Sessions Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/sessions')->name('mentorship.sessions.')->group(function () {
        Route::get('/', [SessionController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship sessions');
        Route::get('/calendar', [SessionController::class, 'calendarView'])->name('calendar')
            ->middleware('permission:view mentorship sessions');
        Route::get('/calendar/events', [SessionController::class, 'calendar'])->name('calendar.events')
            ->middleware('permission:view mentorship sessions');
        Route::get('/create', [SessionController::class, 'create'])->name('create')
            ->middleware('permission:create mentorship sessions');
        Route::post('/', [SessionController::class, 'store'])->name('store')
            ->middleware('permission:create mentorship sessions');
        Route::get('/{session}', [SessionController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship sessions');
        Route::get('/{session}/edit', [SessionController::class, 'edit'])->name('edit')
            ->middleware('permission:edit mentorship sessions');
        Route::put('/{session}', [SessionController::class, 'update'])->name('update')
            ->middleware('permission:edit mentorship sessions');
        Route::delete('/{session}', [SessionController::class, 'destroy'])->name('destroy')
            ->middleware('permission:delete mentorship sessions');
        
        // Actions
        Route::post('/{session}/cancel', [SessionController::class, 'cancel'])->name('cancel')
            ->middleware('permission:cancel mentorship sessions');
        Route::post('/{session}/complete', [SessionController::class, 'complete'])->name('complete')
            ->middleware('permission:complete mentorship sessions');
        Route::post('/{session}/reschedule', [SessionController::class, 'reschedule'])->name('reschedule')
            ->middleware('permission:edit mentorship sessions');
        Route::post('/{session}/attendance', [SessionController::class, 'recordAttendance'])->name('attendance')
            ->middleware('permission:edit mentorship sessions');
        
        // AJAX data endpoints
        Route::get('/assignment/{assignment}', [SessionController::class, 'getForAssignment'])->name('for-assignment');
    });
    
    /*
    |----------------------------------------------------------------------
    | Certificates Management
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/certificates')->name('mentorship.certificates.')->group(function () {
        Route::get('/', [CertificateController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship certificates');
        Route::get('/my-certificates', [CertificateController::class, 'myCertificates'])->name('my-certificates');
        Route::get('/{certificate}', [CertificateController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship certificates');
        Route::get('/{certificate}/download', [CertificateController::class, 'download'])->name('download')
            ->middleware('permission:download mentorship certificates');
        
        // Generation
        Route::post('/generate/{assignment}', [CertificateController::class, 'generate'])->name('generate')
            ->middleware('permission:generate mentorship certificates');
        Route::post('/bulk-generate', [CertificateController::class, 'bulkGenerate'])->name('bulk-generate')
            ->middleware('permission:generate mentorship certificates');
        Route::post('/{certificate}/regenerate', [CertificateController::class, 'regenerate'])->name('regenerate')
            ->middleware('permission:generate mentorship certificates');
        
        // Actions
        Route::post('/{certificate}/revoke', [CertificateController::class, 'revoke'])->name('revoke')
            ->middleware('permission:revoke mentorship certificates');
        
        // Preview
        Route::get('/preview/{assignment}', [CertificateController::class, 'preview'])->name('preview')
            ->middleware('permission:view mentorship certificates');
    });
    
    /*
    |----------------------------------------------------------------------
    | Payments Management (Authenticated)
    |----------------------------------------------------------------------
    */
    Route::prefix('mentorship/payments')->name('mentorship.payments.')->group(function () {
        Route::get('/my-payments', [PaymentController::class, 'myPayments'])->name('my-payments');
        Route::get('/', [PaymentController::class, 'index'])->name('index')
            ->middleware('permission:view mentorship payments');
        Route::get('/{payment}', [PaymentController::class, 'show'])->name('show')
            ->middleware('permission:view mentorship payments');
        Route::get('/{payment}/receipt', [PaymentController::class, 'receipt'])->name('receipt')
            ->middleware('permission:view mentorship payments');
        Route::post('/{payment}/retry', [PaymentController::class, 'retry'])->name('retry');
        Route::get('/statistics', [PaymentController::class, 'statistics'])->name('statistics')
            ->middleware('permission:view mentorship payments');
    });
});
