<?php

use Illuminate\Support\Facades\Route;
use Modules\MedBilling\Http\Controllers\ClaimController;
use Modules\MedBilling\Http\Controllers\InstallController;
use Modules\MedBilling\Http\Controllers\PayerController;
use Modules\MedBilling\Http\Controllers\PolicyController;
use Modules\MedBilling\Http\Controllers\PreAuthController;
use Modules\MedBilling\Http\Controllers\ReportController;
use Modules\MedBilling\Http\Controllers\WorklistController;

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

// Install / Update routes (superadmin-gated inside the controller).
Route::middleware(['web', 'auth'])->prefix('med-billing')->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 (P4-T1). Mirrors the MedNotify middleware stack.
Route::middleware(['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:MedBilling'])
    ->prefix('med-billing')
    ->name('medbill.')
    ->group(function () {
        // Billing worklist (completed appointments → capture → review → bill).
        Route::get('worklist', [WorklistController::class, 'index'])->name('worklist.index');
        Route::post('worklist/capture', [WorklistController::class, 'capture'])->name('worklist.capture');
        Route::get('worklist/review', [WorklistController::class, 'review'])->name('worklist.review');
        Route::post('worklist/bill', [WorklistController::class, 'bill'])->name('worklist.bill');

        // Payers CRUD.
        Route::get('payers', [PayerController::class, 'index'])->name('payers.index');
        Route::get('payers/create', [PayerController::class, 'create'])->name('payers.create');
        Route::post('payers', [PayerController::class, 'store'])->name('payers.store');
        Route::get('payers/{id}/edit', [PayerController::class, 'edit'])->name('payers.edit');
        Route::put('payers/{id}', [PayerController::class, 'update'])->name('payers.update');
        Route::delete('payers/{id}', [PayerController::class, 'destroy'])->name('payers.destroy');

        // Policies CRUD (per patient).
        Route::get('policies', [PolicyController::class, 'index'])->name('policies.index');
        Route::get('policies/create', [PolicyController::class, 'create'])->name('policies.create');
        Route::post('policies', [PolicyController::class, 'store'])->name('policies.store');
        Route::get('policies/{id}/edit', [PolicyController::class, 'edit'])->name('policies.edit');
        Route::put('policies/{id}', [PolicyController::class, 'update'])->name('policies.update');
        Route::delete('policies/{id}', [PolicyController::class, 'destroy'])->name('policies.destroy');

        // Claims (DataTable + detail modal + transitions + print).
        Route::get('claims', [ClaimController::class, 'index'])->name('claims.index');
        Route::get('claims/{id}', [ClaimController::class, 'show'])->name('claims.show');
        Route::get('claims/{id}/print', [ClaimController::class, 'print'])->name('claims.print');
        Route::post('claims/{id}/transition', [ClaimController::class, 'transition'])->name('claims.transition');

        // Pre-auth CRUD.
        Route::get('pre-auth', [PreAuthController::class, 'index'])->name('preauth.index');
        Route::get('pre-auth/create', [PreAuthController::class, 'create'])->name('preauth.create');
        Route::post('pre-auth', [PreAuthController::class, 'store'])->name('preauth.store');
        Route::get('pre-auth/{id}/edit', [PreAuthController::class, 'edit'])->name('preauth.edit');
        Route::put('pre-auth/{id}', [PreAuthController::class, 'update'])->name('preauth.update');
        Route::delete('pre-auth/{id}', [PreAuthController::class, 'destroy'])->name('preauth.destroy');

        // Reports.
        Route::get('reports', [ReportController::class, 'index'])->name('reports.index');
    });
