<?php

use App\Http\Controllers\Api\AppointmentController;
use App\Http\Controllers\Api\AssessmentController;
use App\Http\Controllers\Api\BillController;
use App\Http\Controllers\Api\ConsultationController;
use App\Http\Controllers\Api\DeviceAgentController;
use App\Http\Controllers\Api\DrugController;
use App\Http\Controllers\Api\InvestigationController;
use App\Http\Controllers\Api\PathwayController;
use App\Http\Controllers\Api\PatientController;
use App\Http\Controllers\Api\PrescriptionController;
use App\Http\Controllers\Api\QueueController;
use App\Http\Controllers\Api\ServiceEventController;
use Illuminate\Support\Facades\Route;

Route::get('/health', fn () => response()->json(['status' => 'ok', 'app' => 'care']));

Route::post('/service-events', ServiceEventController::class)->name('api.service-events');

Route::middleware(['care.device', 'throttle:care-device'])->prefix('v1/device-agent')->group(function () {
    Route::post('/heartbeat', [DeviceAgentController::class, 'heartbeat'])->name('api.device-agent.heartbeat');
    Route::post('/vitals', [DeviceAgentController::class, 'storeVitals'])->name('api.device-agent.vitals');
    Route::post('/samples/collect', [DeviceAgentController::class, 'collectSample'])->name('api.device-agent.samples.collect');
});

Route::middleware(['auth:sanctum', 'care.setup'])->prefix('v1')->group(function () {
    Route::get('/patients', [PatientController::class, 'index'])->name('api.patients.index');
    Route::post('/patients', [PatientController::class, 'store'])->name('api.patients.store');
    Route::get('/patients/{patient}', [PatientController::class, 'show'])->name('api.patients.show');
    Route::put('/patients/{patient}', [PatientController::class, 'update'])->name('api.patients.update');
    Route::delete('/patients/{patient}', [PatientController::class, 'destroy'])->name('api.patients.destroy');

    Route::get('/appointments', [AppointmentController::class, 'index'])->name('api.appointments.index');
    Route::post('/appointments', [AppointmentController::class, 'store'])->name('api.appointments.store');
    Route::post('/appointments/walk-in', [AppointmentController::class, 'walkIn'])->name('api.appointments.walk-in');
    Route::get('/appointments/{appointment}', [AppointmentController::class, 'show'])->name('api.appointments.show');
    Route::post('/appointments/{appointment}/check-in', [AppointmentController::class, 'checkIn'])->name('api.appointments.check-in');
    Route::post('/appointments/{appointment}/cancel', [AppointmentController::class, 'cancel'])->name('api.appointments.cancel');

    Route::get('/queue', [QueueController::class, 'index'])->name('api.queue.index');

    Route::post('/appointments/{appointment}/consultation', [ConsultationController::class, 'start'])->name('api.consultations.start');
    Route::get('/consultations/{consultation}', [ConsultationController::class, 'show'])->name('api.consultations.show');
    Route::put('/consultations/{consultation}', [ConsultationController::class, 'update'])->name('api.consultations.update');
    Route::post('/consultations/{consultation}/complete', [ConsultationController::class, 'complete'])->name('api.consultations.complete');

    Route::get('/investigations/catalog', [InvestigationController::class, 'catalog'])->name('api.investigations.catalog');
    Route::get('/investigations', [InvestigationController::class, 'index'])->name('api.investigations.index');
    Route::get('/investigations/queue', [InvestigationController::class, 'queue'])->name('api.investigations.queue');
    Route::post('/consultations/{consultation}/investigations', [InvestigationController::class, 'store'])->name('api.investigations.store');
    Route::get('/investigations/{investigation}', [InvestigationController::class, 'show'])->name('api.investigations.show');
    Route::post('/investigations/{investigation}/collect-sample', [InvestigationController::class, 'collectSample'])->name('api.investigations.collect-sample');
    Route::post('/investigations/{investigation}/results', [InvestigationController::class, 'enterResults'])->name('api.investigations.results');
    Route::post('/investigations/{investigation}/approve', [InvestigationController::class, 'approve'])->name('api.investigations.approve');

    Route::get('/prescriptions', [PrescriptionController::class, 'index'])->name('api.prescriptions.index');
    Route::get('/prescriptions/queue', [PrescriptionController::class, 'queue'])->name('api.prescriptions.queue');
    Route::post('/consultations/{consultation}/prescriptions', [PrescriptionController::class, 'store'])->name('api.prescriptions.store');
    Route::get('/prescriptions/{prescription}', [PrescriptionController::class, 'show'])->name('api.prescriptions.show');
    Route::post('/prescriptions/{prescription}/dispense', [PrescriptionController::class, 'dispense'])->name('api.prescriptions.dispense');

    Route::get('/bills', [BillController::class, 'index'])->name('api.bills.index');
    Route::post('/visits/{visit}/bill', [BillController::class, 'generate'])->name('api.bills.generate');
    Route::get('/bills/{bill}', [BillController::class, 'show'])->name('api.bills.show');
    Route::post('/bills/{bill}/payments', [BillController::class, 'recordPayment'])->name('api.bills.payments.store');

    Route::get('/drugs', [DrugController::class, 'index'])->name('api.drugs.index');
    Route::post('/drugs', [DrugController::class, 'store'])->name('api.drugs.store');
    Route::post('/prescriptions/{prescription}/dispense-stock', [DrugController::class, 'dispense'])->name('api.prescriptions.dispense-stock');

    // Clinical assessments & pathways (gated by assessments_engine rollout)
    Route::get('/assessment-templates', [AssessmentController::class, 'templates'])->name('api.assessment-templates.index');
    Route::get('/patients/{patient}/assessments', [AssessmentController::class, 'index'])->name('api.assessments.index');
    Route::post('/patients/{patient}/assessments', [AssessmentController::class, 'store'])->name('api.assessments.store');
    Route::post('/consultations/{consultation}/assessments', [AssessmentController::class, 'storeForConsultation'])->name('api.consultations.assessments.store');
    Route::get('/assessments/{assessment}', [AssessmentController::class, 'show'])->name('api.assessments.show');
    Route::put('/assessments/{assessment}', [AssessmentController::class, 'update'])->name('api.assessments.update');
    Route::post('/assessments/{assessment}/complete', [AssessmentController::class, 'complete'])->name('api.assessments.complete');
    Route::post('/assessments/{assessment}/cancel', [AssessmentController::class, 'cancel'])->name('api.assessments.cancel');
    Route::get('/assessments/{assessment}/fhir', [AssessmentController::class, 'fhir'])->name('api.assessments.fhir');

    Route::get('/pathways', [PathwayController::class, 'catalog'])->name('api.pathways.catalog');
    Route::get('/patients/{patient}/pathways', [PathwayController::class, 'index'])->name('api.pathways.index');
    Route::post('/patients/{patient}/pathways', [PathwayController::class, 'store'])->name('api.pathways.store');
    Route::post('/patients/{patient}/pathways/{patientPathway}/deactivate', [PathwayController::class, 'deactivate'])->name('api.pathways.deactivate');
    Route::get('/consultations/{consultation}/pathway-suggestions', [PathwayController::class, 'suggestions'])->name('api.consultations.pathway-suggestions');
});
