<?php

use Illuminate\Support\Facades\Route;
use Modules\MedWard\Http\Controllers\AdmissionController;
use Modules\MedWard\Http\Controllers\BedBoardController;
use Modules\MedWard\Http\Controllers\IcuController;
use Modules\MedWard\Http\Controllers\InstallController;
use Modules\MedWard\Http\Controllers\WardController;

/*
|--------------------------------------------------------------------------
| MedWard Back-Office (Blade, inside the ERP UI)
|--------------------------------------------------------------------------
| Session-authenticated routes rendered with the ERP master layout — the SAME
| middleware stack as the MedBilling / MedNotify modules. UI lives under
| `med-ward`. Controllers guard with medward.* permissions.
*/

// Install / Update routes (superadmin-gated inside the controller).
Route::middleware(['web', 'auth'])->prefix('med-ward')->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 (P6-T1/T2/T5). Mirrors the MedBilling middleware stack.
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:MedWard'])
    ->prefix('med-ward')
    ->name('medward.')
    ->group(function () {
        // ── Bed board (real-time-ish) ────────────────────────────────────
        Route::get('board', [BedBoardController::class, 'index'])->name('board.index');
        Route::get('board/data', [BedBoardController::class, 'data'])->name('board.data');
        Route::get('board/available-beds', [BedBoardController::class, 'availableBeds'])->name('board.available_beds');
        Route::post('board/transfer', [BedBoardController::class, 'transfer'])->name('board.transfer');
        Route::post('board/bed-status', [BedBoardController::class, 'updateBedStatus'])->name('board.bed_status');

        // ── Admissions ───────────────────────────────────────────────────
        Route::get('admissions', [AdmissionController::class, 'index'])->name('admissions.index');
        Route::get('admissions/create', [AdmissionController::class, 'create'])->name('admissions.create');
        Route::post('admissions', [AdmissionController::class, 'store'])->name('admissions.store');
        Route::get('admissions/{id}', [AdmissionController::class, 'show'])->name('admissions.show');

        // MAR (per admission).
        Route::post('admissions/{id}/mar/generate', [AdmissionController::class, 'generateMar'])->name('admissions.mar.generate');
        Route::post('mar/{marId}/administer', [AdmissionController::class, 'administerMar'])->name('mar.administer');
        Route::post('mar/{marId}/hold', [AdmissionController::class, 'holdMar'])->name('mar.hold');

        // Fluid balance (per admission).
        Route::post('admissions/{id}/fluid', [AdmissionController::class, 'storeFluid'])->name('admissions.fluid.store');

        // Discharge workflow.
        Route::post('admissions/{id}/discharge/request', [AdmissionController::class, 'requestDischarge'])->name('admissions.discharge.request');
        Route::post('admissions/{id}/discharge', [AdmissionController::class, 'discharge'])->name('admissions.discharge');

        // ── ICU ──────────────────────────────────────────────────────────
        Route::get('icu', [IcuController::class, 'index'])->name('icu.index');
        Route::post('admissions/{id}/flowsheet', [IcuController::class, 'chart'])->name('icu.chart');

        // ── Wards & beds CRUD ────────────────────────────────────────────
        Route::get('wards', [WardController::class, 'index'])->name('wards.index');
        Route::get('wards/create', [WardController::class, 'create'])->name('wards.create');
        Route::post('wards', [WardController::class, 'store'])->name('wards.store');
        Route::get('wards/{id}/edit', [WardController::class, 'edit'])->name('wards.edit');
        Route::put('wards/{id}', [WardController::class, 'update'])->name('wards.update');
        Route::delete('wards/{id}', [WardController::class, 'destroy'])->name('wards.destroy');

        Route::get('wards/{wardId}/beds', [WardController::class, 'beds'])->name('wards.beds');
        Route::get('wards/{wardId}/beds/create', [WardController::class, 'createBed'])->name('beds.create');
        Route::post('beds', [WardController::class, 'storeBed'])->name('beds.store');
        Route::get('beds/{id}/edit', [WardController::class, 'editBed'])->name('beds.edit');
        Route::put('beds/{id}', [WardController::class, 'updateBed'])->name('beds.update');
        Route::delete('beds/{id}', [WardController::class, 'destroyBed'])->name('beds.destroy');
    });
