<?php

use App\Http\Controllers\Admin\AdmissionController;
use App\Http\Controllers\Admin\ApplicationReviewController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\DocumentReviewController;
use App\Http\Controllers\Admin\IntakeController;
use App\Http\Controllers\Admin\PaymentAdminController;
use App\Http\Controllers\Admin\ProgrammeAdminController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SettingsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Administration (PRD 55)
|--------------------------------------------------------------------------
| Every route here is behind authentication, an active account and staff
| status; individual capabilities are then checked per-route by permission.
*/

Route::middleware(['auth', 'active', 'staff'])->group(function () {

    Route::get('/', DashboardController::class)->name('dashboard');

    // ------------------------------------------------------------ Applications
    Route::middleware('permission:applications.view')->group(function () {
        Route::get('/applications', [ApplicationReviewController::class, 'index'])->name('applications.index');
        Route::get('/applications/{application}', [ApplicationReviewController::class, 'show'])->name('applications.show');
        Route::get('/applications/export/csv', [ApplicationReviewController::class, 'export'])
            ->middleware('permission:applications.export')
            ->name('applications.export');
    });

    Route::middleware('permission:applications.review')->group(function () {
        Route::post('/applications/{application}/notes', [ApplicationReviewController::class, 'addNote'])->name('applications.notes.store');
        Route::post('/applications/{application}/status', [ApplicationReviewController::class, 'changeStatus'])->name('applications.status');
        Route::post('/applications/{application}/request-information', [ApplicationReviewController::class, 'requestInformation'])->name('applications.request-information');
        Route::post('/applications/{application}/assign', [ApplicationReviewController::class, 'assign'])
            ->middleware('permission:applications.assign')
            ->name('applications.assign');
    });

    // --------------------------------------------------------------- Documents
    Route::middleware('permission:documents.verify')->group(function () {
        Route::post('/documents/{document}/verify', [DocumentReviewController::class, 'verify'])->name('documents.verify');
        Route::post('/documents/{document}/reject', [DocumentReviewController::class, 'reject'])->name('documents.reject');
    });

    // -------------------------------------------------------------- Admissions
    Route::middleware('permission:admissions.view')->group(function () {
        Route::get('/admissions', [AdmissionController::class, 'index'])->name('admissions.index');
    });

    Route::middleware('permission:applications.approve')->group(function () {
        Route::post('/applications/{application}/admit', [AdmissionController::class, 'admit'])->name('admissions.admit');
    });

    Route::middleware('permission:admissions.generate')->group(function () {
        Route::post('/admissions/{admissionRecord}/regenerate', [AdmissionController::class, 'regenerate'])->name('admissions.regenerate');
        Route::get('/admissions/{admissionRecord}/letter', [AdmissionController::class, 'letter'])->name('admissions.letter');
    });

    // ---------------------------------------------------------------- Payments
    Route::middleware('permission:payments.view')->group(function () {
        Route::get('/payments', [PaymentAdminController::class, 'index'])->name('payments.index');
        Route::get('/payments/{payment}', [PaymentAdminController::class, 'show'])->name('payments.show');
        Route::get('/payments/export/csv', [PaymentAdminController::class, 'export'])
            ->middleware('permission:payments.export')
            ->name('payments.export');
    });

    Route::post('/payments/{payment}/verify', [PaymentAdminController::class, 'verify'])
        ->middleware('permission:payments.verify')
        ->name('payments.verify');

    // -------------------------------------------------------------- Programmes
    Route::middleware('permission:programmes.view')->group(function () {
        Route::get('/programmes', [ProgrammeAdminController::class, 'index'])->name('programmes.index');
        Route::get('/programmes/{programme}/edit', [ProgrammeAdminController::class, 'edit'])
            ->middleware('permission:programmes.update')
            ->name('programmes.edit');
        Route::put('/programmes/{programme}', [ProgrammeAdminController::class, 'update'])
            ->middleware('permission:programmes.update')
            ->name('programmes.update');
    });

    // ----------------------------------------------------------------- Intakes
    Route::middleware('permission:intakes.view')->group(function () {
        Route::get('/intakes', [IntakeController::class, 'index'])->name('intakes.index');
    });

    Route::middleware('permission:intakes.manage')->group(function () {
        Route::post('/intakes/{intake}/status', [IntakeController::class, 'changeStatus'])->name('intakes.status');
    });

    // ----------------------------------------------------------------- Reports
    Route::middleware('permission:reports.view')->group(function () {
        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
    });

    // ---------------------------------------------------------------- Settings
    Route::middleware('permission:settings.manage')->group(function () {
        Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
    });

    Route::get('/audit-logs', [SettingsController::class, 'auditLogs'])
        ->middleware('permission:audit.view')
        ->name('audit-logs');
});
