<?php

use Illuminate\Support\Facades\Route;
use Modules\ClinicLab\Http\Controllers\ClinicLabController;
use Modules\ClinicLab\Http\Controllers\CllCatalogController;
use Modules\ClinicLab\Http\Controllers\CllOutreachController;
use Modules\ClinicLab\Http\Controllers\CllReviewController;
use Modules\ClinicLab\Http\Controllers\CllSpecimenController;
use Modules\ClinicLab\Http\Controllers\InstallController;

/*
 * ClinicLab bridge routes — order LabCore tests from a ClinicCore patient and
 * view results, plus the SIFA P4-T4 lab upgrade (catalog / specimens / reviews /
 * outreach). Self-contained under /clinic-lab so the bridge touches no
 * ClinicCore files.
 */

// Install / update routes (superadmin-gated in the controller). Kept outside the
// module:ClinicLab entitlement group so a superadmin can install before the
// business is entitled — mirrors LabCore's install route placement.
Route::middleware(['web', 'auth'])->prefix('clinic-lab')->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']);
});

Route::middleware(['web', 'auth', 'module:ClinicLab'])->prefix('clinic-lab')->name('clinic-lab.')->group(function () {
    Route::get('/', [ClinicLabController::class, 'index'])->name('index');
    Route::get('patients/{patient}/order', [ClinicLabController::class, 'order'])->name('order');
    Route::post('patients/{patient}/order', [ClinicLabController::class, 'store'])->name('order.store');
    Route::get('patients/{patient}/results', [ClinicLabController::class, 'results'])->name('results');

    // ── SIFA P4-T4: Test catalogue ──
    Route::get('catalog',                 [CllCatalogController::class, 'index'])->name('catalog.index');
    Route::get('catalog/create',          [CllCatalogController::class, 'create'])->name('catalog.create');
    Route::post('catalog',                [CllCatalogController::class, 'store'])->name('catalog.store');
    Route::get('catalog/{id}/edit',       [CllCatalogController::class, 'edit'])->name('catalog.edit');
    Route::put('catalog/{id}',            [CllCatalogController::class, 'update'])->name('catalog.update');
    Route::delete('catalog/{id}',         [CllCatalogController::class, 'destroy'])->name('catalog.destroy');

    // ── SIFA P4-T4: Specimen tracking ──
    Route::get('specimens',               [CllSpecimenController::class, 'index'])->name('specimens.index');
    Route::post('specimens/generate/{worklist}', [CllSpecimenController::class, 'generate'])->name('specimens.generate');
    Route::get('specimens/{id}',          [CllSpecimenController::class, 'show'])->name('specimens.show');
    Route::get('specimens/{id}/barcode',  [CllSpecimenController::class, 'barcode'])->name('specimens.barcode');
    Route::post('specimens/{id}/collect', [CllSpecimenController::class, 'collect'])->name('specimens.collect');
    Route::post('specimens/{id}/receive', [CllSpecimenController::class, 'receive'])->name('specimens.receive');
    Route::post('specimens/{id}/process', [CllSpecimenController::class, 'process'])->name('specimens.process');
    Route::post('specimens/{id}/reject',  [CllSpecimenController::class, 'reject'])->name('specimens.reject');

    // ── SIFA P4-T4: Result validation ──
    Route::get('reviews',                 [CllReviewController::class, 'index'])->name('reviews.index');
    Route::post('reviews/{session}/review', [CllReviewController::class, 'review'])->name('reviews.review');
    Route::post('reviews/{session}/verify', [CllReviewController::class, 'verify'])->name('reviews.verify');

    // ── SIFA P4-T4: Outreach management ──
    Route::get('outreach',                [CllOutreachController::class, 'index'])->name('outreach.index');
    Route::post('outreach/providers',     [CllOutreachController::class, 'storeProvider'])->name('outreach.providers.store');
    Route::put('outreach/providers/{id}', [CllOutreachController::class, 'updateProvider'])->name('outreach.providers.update');
    Route::delete('outreach/providers/{id}', [CllOutreachController::class, 'destroyProvider'])->name('outreach.providers.destroy');
    Route::post('outreach/orders',        [CllOutreachController::class, 'storeOrder'])->name('outreach.orders.store');
    Route::post('outreach/orders/{id}/delivered', [CllOutreachController::class, 'markDelivered'])->name('outreach.orders.delivered');
});
