<?php

use Illuminate\Support\Facades\Route;
use Modules\MedBloodBank\Http\Controllers\CrossmatchController;
use Modules\MedBloodBank\Http\Controllers\DashboardController;
use Modules\MedBloodBank\Http\Controllers\DonationController;
use Modules\MedBloodBank\Http\Controllers\DonorController;
use Modules\MedBloodBank\Http\Controllers\InstallController;
use Modules\MedBloodBank\Http\Controllers\IssueController;

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

// Install / Update routes (superadmin-gated inside the controller).
Route::middleware(['web', 'auth'])->prefix('med-blood-bank')->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-T6). Mirrors the MedBilling middleware stack.
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:MedBloodBank'])
    ->prefix('med-blood-bank')
    ->name('mbb.')
    ->group(function () {
        // Stock dashboard.
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
        Route::post('sweep', [DashboardController::class, 'sweep'])->name('sweep');
        Route::post('notify-low', [DashboardController::class, 'notifyLow'])->name('notify_low');

        // Donors CRUD + contact picker.
        Route::get('donors', [DonorController::class, 'index'])->name('donors.index');
        Route::get('donors/search', [DonorController::class, 'search'])->name('donors.search');
        Route::get('donors/create', [DonorController::class, 'create'])->name('donors.create');
        Route::post('donors', [DonorController::class, 'store'])->name('donors.store');
        Route::get('donors/{id}/edit', [DonorController::class, 'edit'])->name('donors.edit');
        Route::put('donors/{id}', [DonorController::class, 'update'])->name('donors.update');
        Route::delete('donors/{id}', [DonorController::class, 'destroy'])->name('donors.destroy');

        // Donations (entry) + screening.
        Route::get('donations', [DonationController::class, 'index'])->name('donations.index');
        Route::get('donations/create', [DonationController::class, 'create'])->name('donations.create');
        Route::post('donations', [DonationController::class, 'store'])->name('donations.store');
        Route::get('donations/{id}/screen', [DonationController::class, 'screenForm'])->name('donations.screen.form');
        Route::post('donations/{id}/screen', [DonationController::class, 'screen'])->name('donations.screen');

        // Crossmatch screen + pickers/suggestions.
        Route::get('crossmatch', [CrossmatchController::class, 'index'])->name('crossmatch.index');
        Route::get('crossmatch/patients', [CrossmatchController::class, 'patients'])->name('crossmatch.patients');
        Route::get('crossmatch/suggest', [CrossmatchController::class, 'suggest'])->name('crossmatch.suggest');
        Route::post('crossmatch', [CrossmatchController::class, 'store'])->name('crossmatch.store');

        // Issues / transfusions.
        Route::get('issues', [IssueController::class, 'index'])->name('issues.index');
        Route::post('issues', [IssueController::class, 'issue'])->name('issues.issue');
        Route::post('issues/{id}/start', [IssueController::class, 'start'])->name('issues.start');
        Route::get('issues/{id}/complete', [IssueController::class, 'completeForm'])->name('issues.complete.form');
        Route::post('issues/{id}/complete', [IssueController::class, 'complete'])->name('issues.complete');
    });
