<?php

declare(strict_types=1);

use App\Controllers\AuthController;
use App\Controllers\HomeController;
use App\Controllers\InvoiceController;
use App\Controllers\BillingRateController;
use App\Controllers\PaymentController;
use App\Controllers\PdfController;
use App\Controllers\AssignmentController;
use App\Controllers\PatientAccessController;
use App\Controllers\ProfileController;
use App\Controllers\PatientController;
use App\Controllers\SettingsController;
use App\Controllers\TherapistController;
use App\Controllers\UserController;
use App\Controllers\SessionController;
use App\Controllers\AssessmentController;
use App\Controllers\TreatmentPlanController;
use App\Controllers\ReportController;
use App\Controllers\ServiceController;
use App\Controllers\NotificationController;
use App\Controllers\AppointmentController;
use App\Core\Router;
use App\Middleware\AuthMiddleware;
use App\Middleware\GuestMiddleware;
use App\Middleware\PermissionMiddleware;
use App\Middleware\RequestTimingMiddleware;

return static function (Router $router): void {
    $router->get('/', [HomeController::class, 'index'], [new AuthMiddleware(), new RequestTimingMiddleware()], 'home');
    $router->get('/test', [HomeController::class, 'index'], [new AuthMiddleware(), new RequestTimingMiddleware()], 'test');

    $router->get('/login', [AuthController::class, 'loginForm'], [new GuestMiddleware()], 'login');
    $router->post('/login', [AuthController::class, 'login'], [new GuestMiddleware()], 'login.submit');
    $router->post('/logout', [AuthController::class, 'logout'], [new AuthMiddleware()], 'logout');
    $router->get('/forgot-password', [AuthController::class, 'forgotForm'], [new GuestMiddleware()], 'forgot');
    $router->post('/forgot-password', [AuthController::class, 'forgot'], [new GuestMiddleware()], 'forgot.submit');
    $router->get('/reset-password', [AuthController::class, 'resetForm'], [new GuestMiddleware()], 'reset');
    $router->post('/reset-password', [AuthController::class, 'reset'], [new GuestMiddleware()], 'reset.submit');
    $router->get('/setup', [AuthController::class, 'setupForm'], [new GuestMiddleware()], 'setup');
    $router->post('/setup', [AuthController::class, 'setup'], [new GuestMiddleware()], 'setup.submit');

    $router->get('/users', [UserController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('users.view')], 'users.index');
    $router->get('/users/create', [UserController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('users.create')], 'users.create');
    $router->post('/users/create', [UserController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('users.create')], 'users.store');
    $router->get('/users/{id}/edit', [UserController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('users.edit')], 'users.edit');
    $router->post('/users/{id}/edit', [UserController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('users.edit')], 'users.update');
    $router->post('/users/{id}/disable', [UserController::class, 'disable'], [new AuthMiddleware(), new PermissionMiddleware('users.disable')], 'users.disable');

    $router->get('/patients', [PatientController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('patients.view')], 'patients.index');
    $router->get('/patients/create', [PatientController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('patients.create')], 'patients.create');
    $router->post('/patients/create', [PatientController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('patients.create')], 'patients.store');
    $router->get('/patients/{id}', [PatientAccessController::class, 'show'], [new AuthMiddleware(), new PermissionMiddleware('patients.view')], 'patients.show');
    $router->get('/patients/{id}/assignments', [AssignmentController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('patients.assign')], 'assignments.index');
    $router->post('/patients/{id}/assignments', [AssignmentController::class, 'store'], [new AuthMiddleware(), new PermissionMiddleware('patients.assign')], 'assignments.store');
    $router->get('/patients/{id}/edit', [PatientController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('patients.edit')], 'patients.edit');
    $router->post('/patients/{id}/edit', [PatientController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('patients.edit')], 'patients.update');
    $router->post('/assignments/{id}/end', [AssignmentController::class, 'end'], [new AuthMiddleware(), new PermissionMiddleware('patients.assign')], 'assignments.end');
    $router->post('/patients/{id}/delete', [PatientController::class, 'delete'], [new AuthMiddleware(), new PermissionMiddleware('patients.delete')], 'patients.delete');

    $router->get('/therapists', [TherapistController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('therapists.view')], 'therapists.index');
    $router->get('/therapists/create', [TherapistController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('therapists.create')], 'therapists.create');
    $router->post('/therapists/create', [TherapistController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('therapists.create')], 'therapists.store');
    $router->get('/therapists/{id}/edit', [TherapistController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('therapists.edit')], 'therapists.edit');
    $router->post('/therapists/{id}/edit', [TherapistController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('therapists.edit')], 'therapists.update');
    $router->post('/therapists/{id}/delete', [TherapistController::class, 'delete'], [new AuthMiddleware(), new PermissionMiddleware('therapists.delete')], 'therapists.delete');

    $router->get('/appointments', [AppointmentController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('appointments.view')], 'appointments.index');
    $router->get('/appointments/create', [AppointmentController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('appointments.create')], 'appointments.create');
    $router->post('/appointments/create', [AppointmentController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('appointments.create')], 'appointments.store');
    $router->get('/appointments/{id}/edit', [AppointmentController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('appointments.edit')], 'appointments.edit');
    $router->post('/appointments/{id}/edit', [AppointmentController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('appointments.edit')], 'appointments.update');
    $router->post('/appointments/{id}/cancel', [AppointmentController::class, 'cancel'], [new AuthMiddleware(), new PermissionMiddleware('appointments.edit')], 'appointments.cancel');
    $router->post('/appointments/{id}/status', [AppointmentController::class, 'status'], [new AuthMiddleware(), new PermissionMiddleware('appointments.status')], 'appointments.status');

    $router->get('/sessions', [SessionController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('sessions.view')], 'sessions.index');
    $router->get('/sessions/create', [SessionController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('sessions.create')], 'sessions.create');
    $router->post('/sessions/create', [SessionController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('sessions.create')], 'sessions.store');
    $router->get('/sessions/{id}/edit', [SessionController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('sessions.edit')], 'sessions.edit');
    $router->post('/sessions/{id}/edit', [SessionController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('sessions.edit')], 'sessions.update');

    $router->get('/assessments', [AssessmentController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('assessments.view')], 'assessments.index');
    $router->get('/assessments/create', [AssessmentController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('assessments.create')], 'assessments.create');
    $router->post('/assessments/create', [AssessmentController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('assessments.create')], 'assessments.store');
    $router->get('/assessments/{id}/pdf', [PdfController::class, 'assessment'], [new AuthMiddleware(), new PermissionMiddleware('assessments.view')], 'assessments.pdf');
    $router->get('/assessments/{id}/edit', [AssessmentController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('assessments.view')], 'assessments.edit');
    $router->post('/assessments/{id}/edit', [AssessmentController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('assessments.edit')], 'assessments.update');
    $router->post('/assessments/{id}/submit', [AssessmentController::class, 'submit'], [new AuthMiddleware(), new PermissionMiddleware('assessments.submit')], 'assessments.submit');
    $router->post('/assessments/{id}/review', [AssessmentController::class, 'review'], [new AuthMiddleware(), new PermissionMiddleware('assessments.review')], 'assessments.review');
    $router->post('/assessments/{id}/approve', [AssessmentController::class, 'approve'], [new AuthMiddleware(), new PermissionMiddleware('assessments.approve')], 'assessments.approve');

    $router->get('/reports', [ReportController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('reports.view')], 'reports.index');
    $router->get('/reports/create', [ReportController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('reports.create')], 'reports.create');
    $router->post('/reports/create', [ReportController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('reports.create')], 'reports.store');
    $router->get('/reports/{id}/edit', [ReportController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('reports.view')], 'reports.edit');
    $router->post('/reports/{id}/edit', [ReportController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('reports.edit')], 'reports.update');
    $router->post('/reports/{id}/submit', [ReportController::class, 'submit'], [new AuthMiddleware(), new PermissionMiddleware('reports.submit')], 'reports.submit');
    $router->post('/reports/{id}/review', [ReportController::class, 'review'], [new AuthMiddleware(), new PermissionMiddleware('reports.review')], 'reports.review');
    $router->post('/reports/{id}/return', [ReportController::class, 'returnForChanges'], [new AuthMiddleware(), new PermissionMiddleware('reports.review')], 'reports.return');
    $router->post('/reports/{id}/approve', [ReportController::class, 'approve'], [new AuthMiddleware(), new PermissionMiddleware('reports.approve')], 'reports.approve');
    $router->post('/reports/{id}/new-version', [ReportController::class, 'newVersion'], [new AuthMiddleware(), new PermissionMiddleware('reports.edit')], 'reports.new-version');

    $router->get('/services', [ServiceController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('services.view')], 'services.index');
    $router->get('/services/create', [ServiceController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('services.manage')], 'services.create');
    $router->post('/services/create', [ServiceController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('services.manage')], 'services.store');
    $router->get('/services/{id}/edit', [ServiceController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('services.manage')], 'services.edit');
    $router->post('/services/{id}/edit', [ServiceController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('services.manage')], 'services.update');
    $router->post('/services/{id}/delete', [ServiceController::class, 'delete'], [new AuthMiddleware(), new PermissionMiddleware('services.manage')], 'services.delete');

    $router->get('/treatment-plans', [TreatmentPlanController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.view')], 'treatment-plans.index');
    $router->get('/treatment-plans/create', [TreatmentPlanController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.create')], 'treatment-plans.create');
    $router->post('/treatment-plans/create', [TreatmentPlanController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.create')], 'treatment-plans.store');
    $router->get('/treatment-plans/{id}/edit', [TreatmentPlanController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.view')], 'treatment-plans.edit');
    $router->post('/treatment-plans/{id}/edit', [TreatmentPlanController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.edit')], 'treatment-plans.update');
    $router->post('/treatment-plans/{id}/status', [TreatmentPlanController::class, 'status'], [new AuthMiddleware(), new PermissionMiddleware('treatment_plans.manage')], 'treatment-plans.status');

    $router->get('/billing-rates', [BillingRateController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'billing-rates.index');
    $router->post('/billing-rates', [BillingRateController::class, 'save'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'billing-rates.save');
    $router->post('/billing-rates/{id}/delete', [BillingRateController::class, 'delete'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'billing-rates.delete');
    $router->get('/invoices/from-sessions', [InvoiceController::class, 'fromSessionsForm'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.from-sessions');
    $router->post('/invoices/from-sessions', [InvoiceController::class, 'fromClinicalWork'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.from-sessions.store');
    $router->get('/invoices', [InvoiceController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('invoice.view')], 'invoices.index');
    $router->get('/invoices/create', [InvoiceController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.create');
    $router->post('/invoices/create', [InvoiceController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.store');
    $router->get('/invoices/{id}/edit', [InvoiceController::class, 'editForm'], [new AuthMiddleware(), new PermissionMiddleware('invoice.view')], 'invoices.edit');
    $router->post('/invoices/{id}/edit', [InvoiceController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.update');
    $router->post('/invoices/{id}/email', [InvoiceController::class, 'email'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.email');
    $router->post('/invoices/{id}/status', [InvoiceController::class, 'status'], [new AuthMiddleware(), new PermissionMiddleware('invoice.create')], 'invoices.status');

    $router->get('/payments', [PaymentController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('payment.view')], 'payments.index');
    $router->get('/invoices/{id}/payment', [PaymentController::class, 'createForm'], [new AuthMiddleware(), new PermissionMiddleware('payment.create')], 'payments.create');
    $router->post('/invoices/{id}/payment', [PaymentController::class, 'create'], [new AuthMiddleware(), new PermissionMiddleware('payment.create')], 'payments.store');
    $router->post('/payments/{id}/void', [PaymentController::class, 'void'], [new AuthMiddleware(), new PermissionMiddleware('payment.void')], 'payments.void');
    $router->get('/payments/{id}/receipt', [PaymentController::class, 'receipt'], [new AuthMiddleware(), new PermissionMiddleware('receipt.view')], 'receipts.view');
    $router->get('/invoices/{id}/pdf', [PdfController::class, 'invoice'], [new AuthMiddleware(), new PermissionMiddleware('invoice.view')], 'invoices.pdf');
    $router->get('/payments/{id}/receipt/pdf', [PdfController::class, 'receipt'], [new AuthMiddleware(), new PermissionMiddleware('receipt.view')], 'receipts.pdf');
    $router->get('/reports/{id}/pdf', [PdfController::class, 'report'], [new AuthMiddleware(), new PermissionMiddleware('reports.generate')], 'reports.pdf');

    $router->get('/notifications', [NotificationController::class, 'index'], [new AuthMiddleware()], 'notifications.index');
    $router->post('/notifications/{id}/read', [NotificationController::class, 'read'], [new AuthMiddleware()], 'notifications.read');
    $router->post('/notifications/read-all', [NotificationController::class, 'readAll'], [new AuthMiddleware()], 'notifications.read-all');

    $router->get('/settings', [SettingsController::class, 'index'], [new AuthMiddleware(), new PermissionMiddleware('settings.manage')], 'settings');
    $router->post('/settings', [SettingsController::class, 'update'], [new AuthMiddleware(), new PermissionMiddleware('settings.manage')], 'settings.update');
    $router->post('/settings/logo', [SettingsController::class, 'uploadLogo'], [new AuthMiddleware(), new PermissionMiddleware('settings.manage')], 'settings.logo');

    $router->get('/profile', [ProfileController::class, 'show'], [new AuthMiddleware()], 'profile');
    $router->post('/profile/email', [ProfileController::class, 'email'], [new AuthMiddleware()], 'profile.email');
    $router->post('/profile/password', [ProfileController::class, 'password'], [new AuthMiddleware()], 'profile.password');
};
