<?php

use Illuminate\Support\Facades\Route;
use Modules\MedPortal\Http\Controllers\DashboardController;
use Modules\MedPortal\Http\Controllers\DependentController;
use Modules\MedPortal\Http\Controllers\HealthController;
use Modules\MedPortal\Http\Controllers\InstallController;
use Modules\MedPortal\Http\Controllers\MeController;
use Modules\MedPortal\Http\Controllers\MessageController;
use Modules\MedPortal\Http\Controllers\PaymentController;
use Modules\MedPortal\Http\Controllers\QuestionnaireController;
use Modules\MedPortal\Http\Controllers\RecordReleaseController;
use Modules\MedPortal\Http\Controllers\TelemedicineController;

/*
|--------------------------------------------------------------------------
| MedPortal Back-Office (Blade, inside the ERP UI) + thin patient view
|--------------------------------------------------------------------------
| Session-authenticated routes rendered with the ERP master layout — the SAME
| middleware stack as the MedBilling / MedNotify modules. UI lives under
| `med-portal`. Staff controllers guard with mportal.* permissions.
*/

// Install / Update routes (superadmin-gated inside the controller).
Route::middleware(['web', 'auth'])->prefix('med-portal')->group(function () {
    Route::get('/install', [InstallController::class, 'index']);
    Route::post('/install', [InstallController::class, 'install']);
    Route::get('/uninstall', [InstallController::class, 'uninstall']);
    Route::get('/update', [InstallController::class, 'update']);
    Route::get('/repair', [InstallController::class, 'repair']);
});

// Back-office UI (Phase 8). Mirrors the MedBilling middleware stack.
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:MedPortal'])
    ->prefix('med-portal')
    ->name('mportal.')
    ->group(function () {
        // Dashboard.
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

        // Record-release manager.
        Route::get('releases', [RecordReleaseController::class, 'index'])->name('releases.index');
        Route::post('releases/toggle', [RecordReleaseController::class, 'toggle'])->name('releases.toggle');

        // Secure messaging inbox.
        Route::get('messages', [MessageController::class, 'index'])->name('messages.index');
        Route::get('messages/new', [MessageController::class, 'create'])->name('messages.create');
        Route::post('messages/start', [MessageController::class, 'start'])->name('messages.start');
        Route::get('messages/{thread}', [MessageController::class, 'show'])->name('messages.show');
        Route::post('messages/reply', [MessageController::class, 'reply'])->name('messages.reply');

        // Dependents manager (CRUD).
        Route::get('dependents', [DependentController::class, 'index'])->name('dependents.index');
        Route::get('dependents/create', [DependentController::class, 'create'])->name('dependents.create');
        Route::post('dependents', [DependentController::class, 'store'])->name('dependents.store');
        Route::get('dependents/{id}/edit', [DependentController::class, 'edit'])->name('dependents.edit');
        Route::put('dependents/{id}', [DependentController::class, 'update'])->name('dependents.update');
        Route::delete('dependents/{id}', [DependentController::class, 'destroy'])->name('dependents.destroy');

        // Health-tracking viewer.
        Route::get('health', [HealthController::class, 'index'])->name('health.index');
        Route::post('health', [HealthController::class, 'store'])->name('health.store');

        // Questionnaire results + send.
        Route::get('questionnaires', [QuestionnaireController::class, 'index'])->name('questionnaires.index');
        Route::get('questionnaires/send', [QuestionnaireController::class, 'create'])->name('questionnaires.create');
        Route::post('questionnaires/send', [QuestionnaireController::class, 'send'])->name('questionnaires.send');
        Route::get('questionnaires/{id}', [QuestionnaireController::class, 'show'])->name('questionnaires.show');

        // Telemedicine + payment STUBS (backlog).
        Route::post('telemedicine', [TelemedicineController::class, 'store'])->name('telemedicine.store');
        Route::post('payments', [PaymentController::class, 'store'])->name('payments.store');
        Route::post('payments/{id}/mark-paid', [PaymentController::class, 'markPaid'])->name('payments.mark_paid');
    });

/*
|--------------------------------------------------------------------------
| PATIENT-FACING thin web view (spec §D)
|--------------------------------------------------------------------------
| Gated by the `customer` session guard WHEN available; MeController degrades to
| a "portal login required" page when the guard is absent (never breaks). Full
| portal auth remains PortalApi's job — this is API-ready, not a re-auth.
*/
Route::middleware(['web'])
    ->prefix('med-portal/me')
    ->name('mportal.me.')
    ->group(function () {
        Route::get('/', [MeController::class, 'dashboard'])->name('dashboard');
        Route::get('records', [MeController::class, 'records'])->name('records');
        Route::post('health', [MeController::class, 'logHealth'])->name('health');
    });
