<?php

use Illuminate\Support\Facades\Route;
use Modules\MedSurgery\Http\Controllers\BoardController;
use Modules\MedSurgery\Http\Controllers\CaseController;
use Modules\MedSurgery\Http\Controllers\ImplantController;
use Modules\MedSurgery\Http\Controllers\InstallController;

/*
|--------------------------------------------------------------------------
| MedSurgery 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-surgery`. Controllers guard with medsx.* permissions.
*/

// Install / Update routes (superadmin-gated inside the controller).
Route::middleware(['web', 'auth'])->prefix('med-surgery')->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-T4). Mirrors the MedBilling middleware stack.
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:MedSurgery'])
    ->prefix('med-surgery')
    ->name('medsx.')
    ->group(function () {
        // OR scheduling board (week/day grid of OR rooms).
        Route::get('board', [BoardController::class, 'index'])->name('board.index');

        // Cases: list + create + tabbed detail + operations.
        Route::get('cases', [CaseController::class, 'index'])->name('cases.index');
        Route::get('cases/create', [CaseController::class, 'create'])->name('cases.create');
        Route::post('cases', [CaseController::class, 'store'])->name('cases.store');
        Route::get('cases/{id}', [CaseController::class, 'show'])->name('cases.show');
        Route::put('cases/{id}', [CaseController::class, 'update'])->name('cases.update');

        // Status transitions (consent + count gates enforced in SurgeryService).
        Route::post('cases/{id}/transition', [CaseController::class, 'transition'])->name('cases.transition');

        // WHO checklist: tick items + sign a phase.
        Route::post('cases/{id}/checklist/{phase}/items', [CaseController::class, 'checklistItems'])->name('cases.checklist.items');
        Route::post('cases/{id}/checklist/{phase}/sign', [CaseController::class, 'checklistSign'])->name('cases.checklist.sign');

        // Counts (sponge / instrument / needle) upsert.
        Route::post('cases/{id}/count', [CaseController::class, 'saveCount'])->name('cases.count');

        // Op-note attach (fail-soft via MedDocuments).
        Route::post('cases/{id}/op-note', [CaseController::class, 'attachOpNote'])->name('cases.opnote');

        // Implant registry (searchable across all cases).
        Route::get('implants', [ImplantController::class, 'index'])->name('implants.index');
    });
