<?php

use App\Http\Controllers\Applicant\ApplicationController;
use App\Http\Controllers\Applicant\ApplicantDashboardController;
use App\Http\Controllers\Applicant\DocumentController;
use App\Http\Controllers\Applicant\PaymentController;
use App\Http\Controllers\Auth\EmailVerificationController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\PasswordResetController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\PublicSite\AdmissionsController;
use App\Http\Controllers\PublicSite\ContactController;
use App\Http\Controllers\PublicSite\EventController;
use App\Http\Controllers\PublicSite\HomeController;
use App\Http\Controllers\PublicSite\NewsController;
use App\Http\Controllers\PublicSite\PageController;
use App\Http\Controllers\PublicSite\ProgrammeController;
use App\Http\Controllers\PublicSite\SchoolController;
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Public website (PRD 55)
|--------------------------------------------------------------------------
*/

Route::get('/', HomeController::class)->name('home');

Route::get('/programmes', [ProgrammeController::class, 'index'])->name('programmes.index');
Route::get('/programmes/{programme}', [ProgrammeController::class, 'show'])->name('programmes.show');

Route::get('/academics', [SchoolController::class, 'index'])->name('schools.index');
Route::get('/schools/{school}', [SchoolController::class, 'show'])->name('schools.show');

Route::get('/admissions', [AdmissionsController::class, 'index'])->name('admissions');
Route::get('/admissions/requirements', [AdmissionsController::class, 'requirements'])->name('admissions.requirements');
Route::get('/admissions/faqs', [AdmissionsController::class, 'faqs'])->name('faqs');

Route::get('/news', [NewsController::class, 'index'])->name('news.index');
Route::get('/news/{news:slug}', [NewsController::class, 'show'])->name('news.show');

Route::get('/events', [EventController::class, 'index'])->name('events.index');
Route::get('/events/{event}', [EventController::class, 'show'])->name('events.show');

Route::get('/contact', [ContactController::class, 'show'])->name('contact');
Route::post('/contact', [ContactController::class, 'store'])
    ->middleware('throttle:6,1')
    ->name('contact.store');

/*
|--------------------------------------------------------------------------
| Authentication
|--------------------------------------------------------------------------
*/

Route::middleware('guest')->group(function () {
    Route::get('/applicant/register', [RegisterController::class, 'show'])->name('register');
    Route::post('/applicant/register', [RegisterController::class, 'store'])
        ->middleware('throttle:5,1');

    Route::get('/applicant/login', [LoginController::class, 'show'])->name('login');
    Route::post('/applicant/login', [LoginController::class, 'store'])
        ->middleware('throttle:10,1');

    Route::get('/forgot-password', [PasswordResetController::class, 'request'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetController::class, 'email'])
        ->middleware('throttle:5,1')
        ->name('password.email');
    Route::get('/reset-password/{token}', [PasswordResetController::class, 'reset'])->name('password.reset');
    Route::post('/reset-password', [PasswordResetController::class, 'update'])->name('password.update');
});

Route::post('/logout', [LoginController::class, 'destroy'])
    ->middleware('auth')
    ->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/email/verify', [EmailVerificationController::class, 'notice'])->name('verification.notice');
    Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
        ->middleware(['signed', 'throttle:6,1'])
        ->name('verification.verify');
    Route::post('/email/verification-notification', [EmailVerificationController::class, 'send'])
        ->middleware('throttle:6,1')
        ->name('verification.send');
});

/*
|--------------------------------------------------------------------------
| Apply entry point
|--------------------------------------------------------------------------
| Sends visitors to the right place depending on whether they are signed in.
*/

Route::get('/apply', function () {
    return auth()->check()
        ? redirect()->route('applicant.dashboard')
        : redirect()->route('register');
})->name('apply');

/*
|--------------------------------------------------------------------------
| Applicant portal (PRD 55)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'verified', 'active'])
    ->prefix('applicant')
    ->name('applicant.')
    ->group(function () {
        Route::get('/dashboard', ApplicantDashboardController::class)->name('dashboard');

        Route::get('/applications', [ApplicationController::class, 'index'])->name('applications.index');
        Route::post('/applications', [ApplicationController::class, 'store'])->name('applications.store');
        Route::get('/applications/{application}', [ApplicationController::class, 'show'])->name('applications.show');
        Route::get('/applications/{application}/wizard/{step?}', [ApplicationController::class, 'wizard'])->name('applications.wizard');

        Route::get('/documents/{document}/download', [DocumentController::class, 'download'])->name('documents.download');
        Route::get('/documents/{document}/preview', [DocumentController::class, 'preview'])->name('documents.preview');

        Route::get('/applications/{application}/payment', [PaymentController::class, 'show'])->name('payments.show');
        Route::post('/applications/{application}/payment', [PaymentController::class, 'initialize'])->name('payments.initialize');
        Route::get('/payments/{payment}/callback', [PaymentController::class, 'callback'])->name('payments.callback');
        Route::get('/payments/{payment}/receipt', [PaymentController::class, 'receipt'])->name('payments.receipt');

        Route::get('/applications/{application}/letter', [ApplicationController::class, 'admissionLetter'])->name('applications.letter');
        Route::get('/applications/{application}/joining-instructions', [ApplicationController::class, 'joiningInstructions'])->name('applications.joining');

        Route::get('/notifications', [ApplicantDashboardController::class, 'notifications'])->name('notifications');
        Route::post('/notifications/{notification}/read', [ApplicantDashboardController::class, 'markRead'])->name('notifications.read');

        Route::get('/profile', [ApplicantDashboardController::class, 'profile'])->name('profile');
    });

/*
|--------------------------------------------------------------------------
| Payment webhooks
|--------------------------------------------------------------------------
| Deliberately outside the auth + CSRF stack: providers post server-to-server
| and are authenticated by signature instead (PRD 19).
*/

Route::post('/webhooks/payments/{provider}', WebhookController::class)
    ->middleware('throttle:60,1')
    ->name('webhooks.payments');

/*
|--------------------------------------------------------------------------
| CMS pages
|--------------------------------------------------------------------------
| A single-segment catch-all for editor-managed pages. Application prefixes
| are excluded explicitly: admin routes are registered after this file, so
| without the constraint `/admin` would be resolved as a missing CMS page.
*/

Route::get('/{page:slug}', [PageController::class, 'show'])
    ->where('page', '^(?!admin$|applicant$|api$|webhooks$|livewire.*$|storage$|build$|up$)[a-z0-9-]+$')
    ->name('pages.show');
