<?php

use Modules\VetAdmission\Http\Controllers\InstallController;
use Modules\VetAdmission\Http\Controllers\VaWardController;
use Modules\VetAdmission\Http\Controllers\VaCareTaskTypeController;
use Modules\VetAdmission\Http\Controllers\VaAdmissionController;
use Modules\VetAdmission\Http\Controllers\VaMedicationController;
use Modules\VetAdmission\Http\Controllers\VaFluidController;
use Modules\VetAdmission\Http\Controllers\VaProtocolController;
use Modules\VetAdmission\Http\Controllers\VaDischargeController;

// Install / Update routes
Route::middleware(['web', 'auth'])->prefix('vet-admission')->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']);
});

// Admin routes
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:VetAdmission'],
    'prefix'     => 'vet-admission',
], function () {

    // Admissions — static routes before the /{id} wildcard
    Route::get('/admissions',               [VaAdmissionController::class, 'index'])->name('va.admissions.index');
    Route::get('/admissions/list',          [VaAdmissionController::class, 'list'])->name('va.admissions.list');
    Route::get('/admissions/create',        [VaAdmissionController::class, 'create'])->name('va.admissions.create');
    Route::post('/admissions/store',        [VaAdmissionController::class, 'store'])->name('va.admissions.store');
    Route::get('/ward-board',               [VaAdmissionController::class, 'board'])->name('va.admissions.board');
    Route::get('/admissions/{id}',          [VaAdmissionController::class, 'show'])->name('va.admissions.show');
    Route::get('/admissions/{id}/edit',     [VaAdmissionController::class, 'edit'])->name('va.admissions.edit');
    Route::post('/admissions/{id}/update',  [VaAdmissionController::class, 'update'])->name('va.admissions.update');
    Route::post('/admissions/{id}/discharge', [VaAdmissionController::class, 'discharge'])->name('va.admissions.discharge');
    Route::post('/admissions/{id}/care-plan', [VaAdmissionController::class, 'addCarePlan'])->name('va.admissions.care-plan.add');
    Route::delete('/care-plan/{planId}',    [VaAdmissionController::class, 'removeCarePlan'])->name('va.admissions.care-plan.remove');
    Route::post('/admissions/{id}/care-log', [VaAdmissionController::class, 'toggleCareLog'])->name('va.admissions.care-log');
    Route::post('/admissions/{id}/vitals',  [VaAdmissionController::class, 'storeVital'])->name('va.admissions.vitals');
    Route::get('/admissions/{id}/flowsheet',      [VaAdmissionController::class, 'flowsheet'])->name('va.admissions.flowsheet');
    Route::get('/admissions/{id}/flowsheet-data', [VaAdmissionController::class, 'flowsheetData'])->name('va.admissions.flowsheet-data');

    // Medications (M2)
    Route::get('/admissions/{id}/medications',          [VaMedicationController::class, 'index'])->name('va.medications.index');
    Route::get('/admissions/{id}/weight',               [VaMedicationController::class, 'currentWeight'])->name('va.medications.weight');
    Route::post('/admissions/{id}/medications',         [VaMedicationController::class, 'store'])->name('va.medications.store');
    Route::post('/medications/{id}/stop',               [VaMedicationController::class, 'stop'])->name('va.medications.stop');
    Route::post('/medication-administrations/{id}',     [VaMedicationController::class, 'administer'])->name('va.medications.administer');

    // Fluids (M2)
    Route::get('/admissions/{id}/fluids',               [VaFluidController::class, 'index'])->name('va.fluids.index');
    Route::post('/admissions/{id}/fluids',              [VaFluidController::class, 'store'])->name('va.fluids.store');
    Route::post('/fluids/{id}/change-bag',              [VaFluidController::class, 'changeBag'])->name('va.fluids.change-bag');
    Route::post('/fluids/{id}/stop',                    [VaFluidController::class, 'stop'])->name('va.fluids.stop');

    // Vital trends + alerts (M3)
    Route::get('/admissions/{id}/vital-trends',         [VaAdmissionController::class, 'vitalTrends'])->name('va.admissions.vital-trends');
    Route::get('/admissions/{id}/alerts',               [VaAdmissionController::class, 'alerts'])->name('va.admissions.alerts');
    Route::post('/alerts/{id}/ack',                     [VaAdmissionController::class, 'ackAlert'])->name('va.alerts.ack');

    // Protocols (M4) — CRUD on business-owned protocols + clone from system
    Route::get('/protocols',                            [VaProtocolController::class, 'index'])->name('va.protocols.index');
    Route::post('/protocols',                           [VaProtocolController::class, 'store'])->name('va.protocols.store');
    Route::post('/protocols/{id}',                      [VaProtocolController::class, 'update'])->name('va.protocols.update');
    Route::delete('/protocols/{id}',                    [VaProtocolController::class, 'destroy'])->name('va.protocols.destroy');
    Route::post('/protocols/{id}/clone',                [VaProtocolController::class, 'clone'])->name('va.protocols.clone');
    Route::post('/protocols/{id}/items',                [VaProtocolController::class, 'addItem'])->name('va.protocols.items.add');
    Route::delete('/protocol-items/{id}',               [VaProtocolController::class, 'deleteItem'])->name('va.protocols.items.delete');
    Route::get('/admissions/{id}/protocols',            [VaProtocolController::class, 'forAdmission'])->name('va.protocols.for-admission');
    Route::post('/admissions/{id}/protocols/apply',     [VaProtocolController::class, 'apply'])->name('va.protocols.apply');

    // Discharge wizard (M5)
    Route::get('/admissions/{id}/discharge-wizard',     [VaDischargeController::class, 'show'])->name('va.discharge.show');
    Route::post('/admissions/{id}/discharge-preview',   [VaDischargeController::class, 'preview'])->name('va.discharge.preview');
    Route::post('/admissions/{id}/discharge-finalise',  [VaDischargeController::class, 'finalise'])->name('va.discharge.finalise');

    // Wards (settings)
    Route::get('/wards',             [VaWardController::class, 'index'])->name('va.wards.index');
    Route::get('/wards/list',        [VaWardController::class, 'list'])->name('va.wards.list');
    Route::post('/wards/store',      [VaWardController::class, 'store'])->name('va.wards.store');
    Route::get('/wards/{id}/edit',   [VaWardController::class, 'edit'])->name('va.wards.edit');
    Route::post('/wards/{id}/update',[VaWardController::class, 'update'])->name('va.wards.update');
    Route::delete('/wards/{id}',     [VaWardController::class, 'destroy'])->name('va.wards.destroy');

    // Care task types (settings)
    Route::get('/care-task-types',             [VaCareTaskTypeController::class, 'index'])->name('va.care-task-types.index');
    Route::get('/care-task-types/list',        [VaCareTaskTypeController::class, 'list'])->name('va.care-task-types.list');
    Route::post('/care-task-types/store',      [VaCareTaskTypeController::class, 'store'])->name('va.care-task-types.store');
    Route::get('/care-task-types/{id}/edit',   [VaCareTaskTypeController::class, 'edit'])->name('va.care-task-types.edit');
    Route::post('/care-task-types/{id}/update',[VaCareTaskTypeController::class, 'update'])->name('va.care-task-types.update');
    Route::delete('/care-task-types/{id}',     [VaCareTaskTypeController::class, 'destroy'])->name('va.care-task-types.destroy');
});
