<?php

use Modules\VetClinic\Http\Controllers\InstallController;
use Modules\VetClinic\Http\Controllers\DataController;
use Modules\VetClinic\Http\Controllers\VcDashboardController;
use Modules\VetClinic\Http\Controllers\VcDepartmentController;
use Modules\VetClinic\Http\Controllers\VcDoctorController;
use Modules\VetClinic\Http\Controllers\VcPetOwnerController;
use Modules\VetClinic\Http\Controllers\VcPetController;
use Modules\VetClinic\Http\Controllers\VcMedicalConditionController;
use Modules\VetClinic\Http\Controllers\VcAppointmentController;
use Modules\VetClinic\Http\Controllers\VcAppointmentTypeController;
use Modules\VetClinic\Http\Controllers\VcAppointmentReasonController;
use Modules\VetClinic\Http\Controllers\VcClinicLocationController;
use Modules\VetClinic\Http\Controllers\VcPrescriptionController;
use Modules\VetClinic\Http\Controllers\VcVaccinationController;
use Modules\VetClinic\Http\Controllers\VcMedicineController;
use Modules\VetClinic\Http\Controllers\VcReportController;
use Modules\VetClinic\Http\Controllers\VcVaccineTypeController;
use Modules\VetClinic\Http\Controllers\VcMedicalReportController;
use Modules\VetClinic\Http\Controllers\VcBirthDeathRecordController;
use Modules\VetClinic\Http\Controllers\VcSettingsController;
use Modules\VetClinic\Http\Controllers\VcSoapOptionController;
use Modules\VetClinic\Http\Controllers\VcExamController;
use Modules\VetClinic\Http\Controllers\VcOwnerBlockingController;
use Modules\VetClinic\Http\Controllers\VcVitalRangeController;
use Modules\VetClinic\Http\Controllers\VcSystemFindingController;
use Modules\VetClinic\Http\Controllers\VcReminderController;
use Modules\VetClinic\Http\Controllers\VcProductPickerController;
use Modules\VetClinic\Http\Controllers\VcEvaluationController;
use Modules\VetClinic\Http\Controllers\VcEvaluationQuestionController;
use Modules\VetClinic\Http\Controllers\VcAppointmentEditRequestController;
use Modules\VetClinic\Http\Controllers\VcDoctorChangeRequestController;
use Modules\VetClinic\Http\Controllers\VcConsentTemplateController;
use Modules\VetClinic\Http\Controllers\VcConsentController;
use Modules\VetClinic\Http\Controllers\VcConsentVerifyController;
use Modules\VetClinic\Http\Controllers\VcVaccinationVerifyController;
use Modules\VetClinic\Http\Controllers\VcQuotationController;
use Modules\VetClinic\Http\Controllers\VcWellnessController;
use Modules\VetClinic\Http\Controllers\VcImagingController;
use Modules\VetClinic\Http\Controllers\VcRoleLandingController;
use Modules\VetClinic\Http\Controllers\VcControlledRegisterController;
use Modules\VetClinic\Http\Controllers\VcAuthorizedVetController;
use Modules\VetClinic\Http\Controllers\VcConfidentialNoteController;
use Modules\VetClinic\Http\Controllers\DevLoginController;
use Modules\VetClinic\Http\Controllers\VcAiController;

// Public routes (no auth required)
Route::middleware(['web'])->prefix('vet-clinic')->group(function () {
    Route::get('/confirm-email/{token}', [VcPetOwnerController::class, 'confirmEmail'])->name('vc.pet-owners.confirm-email');
    Route::get('/verify-consent/{hash}', [VcConsentVerifyController::class, 'show'])->name('vc.consents.verify');
    Route::get('/verify-vaccinations/{token}', [VcVaccinationVerifyController::class, 'show'])->name('vc.vaccinations.verify');

    // Public vet-quotation approval
    Route::get('/quotations/{token}',          [VcQuotationController::class, 'publicView'])->name('vc.quotations.public.view');
    Route::post('/quotations/{token}/approve', [VcQuotationController::class, 'publicApprove'])->name('vc.quotations.public.approve');
    Route::post('/quotations/{token}/reject',  [VcQuotationController::class, 'publicReject'])->name('vc.quotations.public.reject');
});

// Dev quick-login (no auth) — hard-gated inside the controller by
// config('vetclinic.dev_login'); 404s unless VET_DEV_LOGIN=true in .env.
Route::middleware(['web'])->group(function () {
    Route::get('/dev-login',       [DevLoginController::class, 'index'])->name('vc.dev-login.index');
    Route::get('/dev-login/{key}', [DevLoginController::class, 'login'])->name('vc.dev-login.login');
});

// Install / Update routes
Route::middleware(['web', 'auth'])->prefix('vet-clinic')->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']);
});

// Admin routes
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:VetClinic'],
    'prefix'     => 'vet-clinic',
], function () {

    // Dashboard
    Route::get('/', [VcDashboardController::class, 'index'])->name('vc.dashboard');

    // Departments
    Route::get('/departments',          [VcDepartmentController::class, 'index'])->name('vc.departments.index');
    Route::get('/departments/list',     [VcDepartmentController::class, 'list'])->name('vc.departments.list');
    Route::post('/departments/store',   [VcDepartmentController::class, 'store'])->name('vc.departments.store');
    Route::get('/departments/{id}/edit',[VcDepartmentController::class, 'edit'])->name('vc.departments.edit');
    Route::post('/departments/{id}/update', [VcDepartmentController::class, 'update'])->name('vc.departments.update');
    Route::delete('/departments/{id}',  [VcDepartmentController::class, 'destroy'])->name('vc.departments.destroy');

    // Doctors
    Route::get('/doctors/search',                    [VcDoctorController::class, 'search'])->name('vc.doctors.search');
    Route::get('/doctors/schedule-summary',          [VcDoctorController::class, 'scheduleSummary'])->name('vc.doctors.schedule-summary');
    Route::get('/doctors/{id}/schedules',            [VcDoctorController::class, 'schedules'])->name('vc.doctors.schedules');
    Route::post('/doctors/{id}/schedules/store',     [VcDoctorController::class, 'storeSchedule'])->name('vc.doctors.schedules.store');
    Route::post('/doctors/schedules/{sid}/update',   [VcDoctorController::class, 'updateSchedule'])->name('vc.doctors.schedules.update');
    Route::delete('/doctors/schedules/{sid}',        [VcDoctorController::class, 'destroySchedule'])->name('vc.doctors.schedules.destroy');
    Route::get('/doctors',              [VcDoctorController::class, 'index'])->name('vc.doctors.index');
    Route::get('/doctors/list',         [VcDoctorController::class, 'list'])->name('vc.doctors.list');
    Route::post('/doctors/store',       [VcDoctorController::class, 'store'])->name('vc.doctors.store');
    Route::get('/doctors/{id}/edit',    [VcDoctorController::class, 'edit'])->name('vc.doctors.edit');
    Route::post('/doctors/{id}/update', [VcDoctorController::class, 'update'])->name('vc.doctors.update');
    Route::delete('/doctors/{id}',      [VcDoctorController::class, 'destroy'])->name('vc.doctors.destroy');

    // Pet Owners  (static routes MUST come before /{id} wildcard)
    Route::get('/pet-owners',                    [VcPetOwnerController::class, 'index'])->name('vc.pet-owners.index');
    Route::get('/pet-owners/list',               [VcPetOwnerController::class, 'list'])->name('vc.pet-owners.list');
    Route::get('/pet-owners/create',             [VcPetOwnerController::class, 'create'])->name('vc.pet-owners.create');
    Route::get('/pet-owners/search',             [VcPetOwnerController::class, 'search'])->name('vc.pet-owners.search');
    Route::post('/pet-owners/store',             [VcPetOwnerController::class, 'store'])->name('vc.pet-owners.store');
    Route::get('/pet-owners/{id}',               [VcPetOwnerController::class, 'show'])->name('vc.pet-owners.show');
    Route::get('/pet-owners/{id}/edit',          [VcPetOwnerController::class, 'edit'])->name('vc.pet-owners.edit');
    Route::post('/pet-owners/{id}/update',          [VcPetOwnerController::class, 'update'])->name('vc.pet-owners.update');
    Route::post('/pet-owners/{id}/resend-confirmation', [VcPetOwnerController::class, 'resendConfirmation'])->name('vc.pet-owners.resend-confirmation');

    // Pets
    Route::get('/pets',                   [VcPetController::class, 'index'])->name('vc.pets.index');
    Route::get('/pets/list',              [VcPetController::class, 'list'])->name('vc.pets.list');
    Route::get('/pets/create',            [VcPetController::class, 'create'])->name('vc.pets.create');
    Route::post('/pets/store',            [VcPetController::class, 'store'])->name('vc.pets.store');
    Route::get('/pets/{id}',              [VcPetController::class, 'show'])->name('vc.pets.show');
    Route::get('/pets/{id}/edit',         [VcPetController::class, 'edit'])->name('vc.pets.edit');
    Route::post('/pets/{id}/update',      [VcPetController::class, 'update'])->name('vc.pets.update');
    Route::delete('/pets/{id}',           [VcPetController::class, 'destroy'])->name('vc.pets.destroy');
    Route::post('/pets/{id}/mark-deceased', [VcPetController::class, 'markDeceased'])->name('vc.pets.mark-deceased');
    Route::get('/pets/{id}/conditions',   [VcPetController::class, 'getConditions'])->name('vc.pets.conditions');
    Route::post('/pets/{id}/conditions/add', [VcPetController::class, 'addCondition'])->name('vc.pets.conditions.add');
    Route::delete('/pets/conditions/{conditionId}', [VcPetController::class, 'removeCondition'])->name('vc.pets.conditions.remove');
    Route::post('/pets/conditions/{conditionId}/update', [VcPetController::class, 'updateCondition'])->name('vc.pets.conditions.update');
    Route::get('/pets/by-owner/{contactId}', [VcPetController::class, 'byOwner'])->name('vc.pets.by-owner');
    Route::get('/pets/{id}/preferred-doctor', [VcPetController::class, 'preferredDoctor'])->name('vc.pets.preferred-doctor');
    Route::get('/pets/{id}/print-vaccinations', [VcPetController::class, 'printVaccinations'])->name('vc.pets.print-vaccinations');

    // Medical Conditions (catalogue)
    Route::get('/medical-conditions',             [VcMedicalConditionController::class, 'index'])->name('vc.medical-conditions.index');
    Route::get('/medical-conditions/list',        [VcMedicalConditionController::class, 'list'])->name('vc.medical-conditions.list');
    Route::post('/medical-conditions/store',      [VcMedicalConditionController::class, 'store'])->name('vc.medical-conditions.store');
    Route::get('/medical-conditions/{id}/edit',   [VcMedicalConditionController::class, 'edit'])->name('vc.medical-conditions.edit');
    Route::post('/medical-conditions/{id}/update',[VcMedicalConditionController::class, 'update'])->name('vc.medical-conditions.update');
    Route::delete('/medical-conditions/{id}',     [VcMedicalConditionController::class, 'destroy'])->name('vc.medical-conditions.destroy');
    Route::get('/medical-conditions/search',      [VcMedicalConditionController::class, 'search'])->name('vc.medical-conditions.search');

    // Appointments — AJAX helpers (static routes before wildcard)
    Route::get('/appointments/locations-by-date',  [VcAppointmentController::class, 'locationsByDate'])->name('vc.appointments.locations-by-date');
    Route::get('/appointments/depts-by-location',  [VcAppointmentController::class, 'deptsByLocation'])->name('vc.appointments.depts-by-location');
    Route::get('/appointments/doctors-by-dept',    [VcAppointmentController::class, 'doctorsByDept'])->name('vc.appointments.doctors-by-dept');
    Route::get('/appointments/slots',              [VcAppointmentController::class, 'availableSlots'])->name('vc.appointments.slots');
    Route::get('/appointments/search-invoices',    [VcAppointmentController::class, 'searchInvoices'])->name('vc.appointments.search-invoices');
    Route::get('/appointments',                    [VcAppointmentController::class, 'index'])->name('vc.appointments.index');
    Route::get('/appointments/calendar',           [VcAppointmentController::class, 'calendar'])->name('vc.appointments.calendar');
    Route::get('/appointments/list',               [VcAppointmentController::class, 'list'])->name('vc.appointments.list');
    Route::get('/appointments/events',             [VcAppointmentController::class, 'events'])->name('vc.appointments.events');
    Route::get('/appointments/create',             [VcAppointmentController::class, 'create'])->name('vc.appointments.create');
    Route::post('/appointments/store',             [VcAppointmentController::class, 'store'])->name('vc.appointments.store');
    Route::get('/appointments/{id}',               [VcAppointmentController::class, 'show'])->name('vc.appointments.show');
    Route::get('/appointments/{id}/print',         [VcAppointmentController::class, 'print'])->name('vc.appointments.print');
    Route::get('/appointments/{id}/edit',          [VcAppointmentController::class, 'edit'])->name('vc.appointments.edit');
    Route::post('/appointments/{id}/update',       [VcAppointmentController::class, 'update'])->name('vc.appointments.update');
    Route::post('/appointments/{id}/status',       [VcAppointmentController::class, 'updateStatus'])->name('vc.appointments.status');
    Route::delete('/appointments/{id}',            [VcAppointmentController::class, 'destroy'])->name('vc.appointments.destroy');
    Route::post('/appointments/{id}/notes/store',   [VcAppointmentController::class, 'storeNote'])->name('vc.appointments.notes.store');

    // ── Item 1.4 - structured Objective exam endpoints ─────────────
    Route::get   ('/appointments/{id}/exam/templates',                 [VcExamController::class, 'templates'])    ->name('vc.appointments.exam.templates');
    Route::get   ('/appointments/{id}/exam/findings',                  [VcExamController::class, 'findings'])     ->name('vc.appointments.exam.findings');
    Route::post  ('/appointments/{id}/exam/findings',                  [VcExamController::class, 'saveFinding'])  ->name('vc.appointments.exam.findings.save');
    Route::delete('/appointments/{id}/exam/findings/{findingId}',      [VcExamController::class, 'deleteFinding'])->name('vc.appointments.exam.findings.delete');

    Route::post('/appointments/{id}/consultation',  [VcAppointmentController::class, 'saveConsultation'])->name('vc.appointments.consultation');
    Route::post('/appointments/{id}/documents',     [VcAppointmentController::class, 'uploadDocument'])->name('vc.appointments.documents.upload');
    Route::delete('/appointments/{id}/documents/{docId}', [VcAppointmentController::class, 'deleteDocument'])->name('vc.appointments.documents.delete');
    Route::get('/appointments/{id}/launch-pos',    [VcAppointmentController::class, 'launchPos'])->name('vc.appointments.launch-pos');
    Route::post('/appointments/{id}/link-invoice', [VcAppointmentController::class, 'linkInvoice'])->name('vc.appointments.link-invoice');
    Route::post('/prescriptions/from-consultation', [VcPrescriptionController::class, 'storeFromConsultation'])->name('vc.prescriptions.from-consultation');

    // Appointment Types (settings)
    Route::get('/appointment-types',              [VcAppointmentTypeController::class, 'index'])->name('vc.apt-types.index');
    Route::get('/appointment-types/list',         [VcAppointmentTypeController::class, 'list'])->name('vc.apt-types.list');
    Route::post('/appointment-types/store',       [VcAppointmentTypeController::class, 'store'])->name('vc.apt-types.store');
    Route::get('/appointment-types/{id}/edit',    [VcAppointmentTypeController::class, 'edit'])->name('vc.apt-types.edit');
    Route::post('/appointment-types/{id}/update', [VcAppointmentTypeController::class, 'update'])->name('vc.apt-types.update');
    Route::delete('/appointment-types/{id}',      [VcAppointmentTypeController::class, 'destroy'])->name('vc.apt-types.destroy');

    // Appointment Reasons (settings)
    Route::get('/appointment-reasons',              [VcAppointmentReasonController::class, 'index'])->name('vc.apt-reasons.index');
    Route::get('/appointment-reasons/list',         [VcAppointmentReasonController::class, 'list'])->name('vc.apt-reasons.list');
    Route::post('/appointment-reasons/store',       [VcAppointmentReasonController::class, 'store'])->name('vc.apt-reasons.store');
    Route::get('/appointment-reasons/{id}/edit',    [VcAppointmentReasonController::class, 'edit'])->name('vc.apt-reasons.edit');
    Route::post('/appointment-reasons/{id}/update', [VcAppointmentReasonController::class, 'update'])->name('vc.apt-reasons.update');
    Route::delete('/appointment-reasons/{id}',      [VcAppointmentReasonController::class, 'destroy'])->name('vc.apt-reasons.destroy');

    // Module Settings
    Route::get('/settings',              [VcSettingsController::class, 'index'])->name('vc.settings.index');
    Route::post('/settings/save',        [VcSettingsController::class, 'save'])->name('vc.settings.save');

    // PWA Portal Brand (settings → pet-owner PWA branding)
    Route::get('/portal-brand',          [\Modules\VetClinic\Http\Controllers\VcPortalBrandController::class, 'index'])->name('vc.portal-brand.index');
    Route::post('/portal-brand/save',    [\Modules\VetClinic\Http\Controllers\VcPortalBrandController::class, 'save'])->name('vc.portal-brand.save');
    Route::post('/portal-brand/logo',    [\Modules\VetClinic\Http\Controllers\VcPortalBrandController::class, 'uploadLogo'])->name('vc.portal-brand.logo');
    Route::post('/portal-brand/rebuild', [\Modules\VetClinic\Http\Controllers\VcPortalBrandController::class, 'rebuildPwa'])->name('vc.portal-brand.rebuild');

    // PWA Engagement (analytics + broadcast push + per-owner detail)
    Route::get('/portal-engagement',                       [\Modules\VetClinic\Http\Controllers\VcPortalEngagementController::class, 'index'])->name('vc.portal-engagement.index');
    Route::post('/portal-engagement/broadcast',            [\Modules\VetClinic\Http\Controllers\VcPortalEngagementController::class, 'broadcast'])->name('vc.portal-engagement.broadcast');
    Route::get('/portal-engagement/owner/{id}',            [\Modules\VetClinic\Http\Controllers\VcPortalEngagementController::class, 'ownerDetail'])->where('id', '[0-9]+')->name('vc.portal-engagement.owner');
    Route::post('/portal-engagement/owner/{id}/sign-out',  [\Modules\VetClinic\Http\Controllers\VcPortalEngagementController::class, 'signOutEverywhere'])->where('id', '[0-9]+')->name('vc.portal-engagement.owner.sign-out');

    // SOAP Options (settings + AJAX)
    Route::get('/soap-options',               [VcSoapOptionController::class, 'index'])->name('vc.soap-options.index');
    Route::get('/soap-options/data',          [VcSoapOptionController::class, 'data'])->name('vc.soap-options.data');
    Route::get('/soap-options/search',        [VcSoapOptionController::class, 'search'])->name('vc.soap-options.search');
    Route::post('/soap-options/store',            [VcSoapOptionController::class, 'store'])->name('vc.soap-options.store');
    Route::post('/soap-options/group/update',     [VcSoapOptionController::class, 'updateGroup'])->name('vc.soap-options.group.update');
    Route::post('/soap-options/{id}/update',      [VcSoapOptionController::class, 'update'])->name('vc.soap-options.update');
    Route::delete('/soap-options/{id}',           [VcSoapOptionController::class, 'destroy'])->name('vc.soap-options.destroy');
    Route::post('/soap-options/{id}/toggle',      [VcSoapOptionController::class, 'toggleStatus'])->name('vc.soap-options.toggle');

    // Clinic Locations (settings)
    Route::get('/locations',              [VcClinicLocationController::class, 'index'])->name('vc.locations.index');
    Route::get('/locations/list',         [VcClinicLocationController::class, 'list'])->name('vc.locations.list');
    Route::post('/locations/store',       [VcClinicLocationController::class, 'store'])->name('vc.locations.store');
    Route::get('/locations/{id}/edit',    [VcClinicLocationController::class, 'edit'])->name('vc.locations.edit');
    Route::post('/locations/{id}/update', [VcClinicLocationController::class, 'update'])->name('vc.locations.update');
    Route::delete('/locations/{id}',      [VcClinicLocationController::class, 'destroy'])->name('vc.locations.destroy');

    // Vaccinations
    Route::get('/vaccinations',                     [VcVaccinationController::class, 'index'])->name('vc.vaccinations.index');
    Route::get('/vaccinations/list',                [VcVaccinationController::class, 'list'])->name('vc.vaccinations.list');
    Route::post('/vaccinations/store',              [VcVaccinationController::class, 'store'])->name('vc.vaccinations.store');
    Route::get('/vaccinations/{id}/edit',           [VcVaccinationController::class, 'edit'])->name('vc.vaccinations.edit');
    Route::post('/vaccinations/{id}/update',        [VcVaccinationController::class, 'update'])->name('vc.vaccinations.update');
    Route::delete('/vaccinations/{id}',             [VcVaccinationController::class, 'destroy'])->name('vc.vaccinations.destroy');
    Route::get('/vaccine-types/search',             [VcVaccinationController::class, 'searchVaccineTypes'])->name('vc.vaccine-types.search');
    Route::post('/vaccinations/{id}/remind',              [VcVaccinationController::class, 'markReminded'])->name('vc.vaccinations.remind');
    Route::post('/vaccinations/{id}/send-email-reminder', [VcVaccinationController::class, 'sendEmailReminder'])->name('vc.vaccinations.send-email-reminder');
    Route::post('/vaccinations/{id}/waive',               [VcVaccinationController::class, 'waive'])->name('vc.vaccinations.waive');
    Route::post('/vaccinations/{id}/unwaive',             [VcVaccinationController::class, 'unwaive'])->name('vc.vaccinations.unwaive');

    // Prescriptions
    Route::get('/prescriptions',                    [VcPrescriptionController::class, 'index'])->name('vc.prescriptions.index');
    Route::get('/prescriptions/list',               [VcPrescriptionController::class, 'list'])->name('vc.prescriptions.list');
    Route::get('/prescriptions/create',             [VcPrescriptionController::class, 'create'])->name('vc.prescriptions.create');
    Route::post('/prescriptions/store',             [VcPrescriptionController::class, 'store'])->name('vc.prescriptions.store');
    Route::get('/prescriptions/{id}',               [VcPrescriptionController::class, 'show'])->name('vc.prescriptions.show');
    Route::get('/prescriptions/{id}/edit',          [VcPrescriptionController::class, 'edit'])->name('vc.prescriptions.edit');
    Route::post('/prescriptions/{id}/update',       [VcPrescriptionController::class, 'update'])->name('vc.prescriptions.update');
    Route::delete('/prescriptions/{id}',            [VcPrescriptionController::class, 'destroy'])->name('vc.prescriptions.destroy');
    Route::get('/prescriptions/{id}/print',         [VcPrescriptionController::class, 'print'])->name('vc.prescriptions.print');
    Route::get('/prescriptions/items/{itemId}/dispense-options', [VcPrescriptionController::class, 'dispenseOptions'])->name('vc.prescriptions.dispense-options');
    Route::post('/prescriptions/items/{itemId}/dispense',         [VcPrescriptionController::class, 'dispenseItem'])->name('vc.prescriptions.dispense');
    // AI veterinary prescription safety check (interaction / species-toxicity / contraindication).
    Route::post('/prescriptions/check-safety', [VcPrescriptionController::class, 'checkSafety'])->name('vc.prescriptions.check-safety');

    // Medicines (pharmacy catalogue)
    Route::get('/medicines',                        [VcMedicineController::class, 'index'])->name('vc.medicines.index');
    Route::get('/medicines/list',                   [VcMedicineController::class, 'list'])->name('vc.medicines.list');
    Route::post('/medicines/store',                 [VcMedicineController::class, 'store'])->name('vc.medicines.store');
    Route::get('/medicines/{id}/edit',              [VcMedicineController::class, 'edit'])->name('vc.medicines.edit');
    Route::post('/medicines/{id}/update',           [VcMedicineController::class, 'update'])->name('vc.medicines.update');
    Route::delete('/medicines/{id}',                [VcMedicineController::class, 'destroy'])->name('vc.medicines.destroy');
    Route::get('/medicines/product-search',          [VcMedicineController::class, 'productSearch'])->name('vc.medicines.product-search');
    Route::get('/medicines/search',                 [VcMedicineController::class, 'search'])->name('vc.medicines.search');

    // Reports
    Route::get('/reports',                          [VcReportController::class, 'index'])->name('vc.reports.index');
    Route::get('/reports/kpi',                      [VcReportController::class, 'kpiDashboard'])->name('vc.reports.kpi');
    Route::get('/reports/appointments',             [VcReportController::class, 'appointments'])->name('vc.reports.appointments');
    Route::get('/reports/critical-check',            [VcReportController::class, 'criticalCheck'])->name('vc.reports.critical-check');

    // ── Item 3.1 / 3.2 / 2.2 — Owner blocking ──────────────────────
    // Item #2 - preferred doctor change endpoints (owner-level + pet-level)
    Route::post('/pet-owners/{id}/preferred-doctor', [VcPetOwnerController::class, 'setPreferredDoctor'])->name('vc.pet-owners.set-preferred-doctor');
    Route::post('/pets/{id}/preferred-doctor',       [VcPetController::class, 'setPreferredDoctor'])    ->name('vc.pets.set-preferred-doctor');
    Route::post('/pet-owners/{id}/whatsapp',         [VcPetOwnerController::class, 'setWhatsappState'])->name('vc.pet-owners.set-whatsapp');
    Route::post('/pet-owners/{id}/send-message',     [VcPetOwnerController::class, 'sendMessage'])     ->name('vc.pet-owners.send-message');
    // Bulk push to a multi-selected set of pet owners on the index page.
    Route::post('/pet-owners/bulk-push',              [VcPetOwnerController::class, 'bulkPush'])        ->name('vc.pet-owners.bulk-push');
    Route::post('/pet-owners/bulk-push/preview',      [VcPetOwnerController::class, 'bulkPushPreview'])->name('vc.pet-owners.bulk-push.preview');
    // Item #8 - professional PDF clinical history (dompdf, with QR)
    Route::get ('/pets/{id}/clinical-history.pdf',   [VcPetController::class, 'printClinicalHistoryPdf'])->name('vc.pets.clinical-history.pdf');

    // Item #1 - vital-range admin CRUD
    Route::get   ('/vital-ranges',                   [VcVitalRangeController::class, 'index'])  ->name('vc.vital-ranges.index');
    Route::get   ('/vital-ranges/create',            [VcVitalRangeController::class, 'create']) ->name('vc.vital-ranges.create');
    Route::post  ('/vital-ranges',                   [VcVitalRangeController::class, 'store'])  ->name('vc.vital-ranges.store');
    Route::get   ('/vital-ranges/{id}/edit',         [VcVitalRangeController::class, 'edit'])   ->name('vc.vital-ranges.edit');
    Route::post  ('/vital-ranges/{id}',              [VcVitalRangeController::class, 'update']) ->name('vc.vital-ranges.update');
    Route::delete('/vital-ranges/{id}',              [VcVitalRangeController::class, 'destroy'])->name('vc.vital-ranges.destroy');

    // Item #4a - body-system finding catalogues (admin CRUD + JSON for appointment page)
    Route::get   ('/system-findings',                                [VcSystemFindingController::class, 'index'])    ->name('vc.system-findings.index');
    Route::get   ('/system-findings/catalogue/{systemKey}.json',     [VcSystemFindingController::class, 'catalogue'])->name('vc.system-findings.catalogue');
    Route::get   ('/system-findings/{systemKey}',                    [VcSystemFindingController::class, 'list'])    ->name('vc.system-findings.list');
    Route::get   ('/system-findings/{systemKey}/create',             [VcSystemFindingController::class, 'create'])  ->name('vc.system-findings.create');
    Route::post  ('/system-findings',                                [VcSystemFindingController::class, 'store'])   ->name('vc.system-findings.store');
    Route::get   ('/system-findings/edit/{id}',                      [VcSystemFindingController::class, 'edit'])    ->name('vc.system-findings.edit');
    Route::post  ('/system-findings/edit/{id}',                      [VcSystemFindingController::class, 'update'])  ->name('vc.system-findings.update');
    Route::delete('/system-findings/{id}',                           [VcSystemFindingController::class, 'destroy']) ->name('vc.system-findings.destroy');

    Route::get ('/owner-blocking',                    [VcOwnerBlockingController::class, 'index'])      ->name('vc.owner-blocking.index');
    Route::get ('/owner-blocking/settings',           [VcOwnerBlockingController::class, 'settings'])   ->name('vc.owner-blocking.settings');
    Route::post('/owner-blocking/settings',           [VcOwnerBlockingController::class, 'saveSettings'])->name('vc.owner-blocking.save');
    Route::post('/owner-blocking/{id}/block',         [VcOwnerBlockingController::class, 'block'])      ->name('vc.owner-blocking.block');
    Route::post('/owner-blocking/{id}/unblock',       [VcOwnerBlockingController::class, 'unblock'])    ->name('vc.owner-blocking.unblock');
    // Item 1.3 - live vital ranges for inline form warnings
    Route::get('/appointments/{id}/vital-ranges',    [VcAppointmentController::class, 'vitalRanges'])->name('vc.appointments.vital-ranges');
    Route::get('/reports/vaccinations-due',         [VcReportController::class, 'vaccinationsDue'])->name('vc.reports.vaccinations-due');
    Route::get('/reports/pet-registrations',        [VcReportController::class, 'petRegistrations'])->name('vc.reports.pet-registrations');
    Route::get('/reports/doctor-activity',          [VcReportController::class, 'doctorActivity'])->name('vc.reports.doctor-activity');
    Route::get('/reports/evaluations',              [VcReportController::class, 'evaluations'])->name('vc.reports.evaluations');
    Route::get('/reports/time-tracking',            [VcReportController::class, 'timeTracking'])->name('vc.reports.time-tracking');
    Route::get('/reports/owner-geography',          [VcReportController::class, 'ownerGeography'])->name('vc.reports.owner-geography');
    Route::get('/reports/visit-patterns',           [VcReportController::class, 'visitPatterns'])->name('vc.reports.visit-patterns');
    Route::get('/reports/inactive-owners',          [VcReportController::class, 'inactiveOwners'])->name('vc.reports.inactive-owners');
    Route::get('/reports/preferred-doctor-changes', [VcReportController::class, 'preferredDoctorChanges'])->name('vc.reports.preferred-doctor-changes');

    // Evaluation Questions (settings)
    Route::get('/evaluation-questions',              [VcEvaluationQuestionController::class, 'index'])->name('vc.evaluation-questions.index');
    Route::get('/evaluation-questions/list',         [VcEvaluationQuestionController::class, 'list'])->name('vc.evaluation-questions.list');
    Route::post('/evaluation-questions/store',       [VcEvaluationQuestionController::class, 'store'])->name('vc.evaluation-questions.store');
    Route::get('/evaluation-questions/{id}/edit',    [VcEvaluationQuestionController::class, 'edit'])->name('vc.evaluation-questions.edit');
    Route::post('/evaluation-questions/{id}/update', [VcEvaluationQuestionController::class, 'update'])->name('vc.evaluation-questions.update');
    Route::delete('/evaluation-questions/{id}',      [VcEvaluationQuestionController::class, 'destroy'])->name('vc.evaluation-questions.destroy');

    // Evaluations (per appointment)
    Route::get('/appointments/{id}/evaluation/form',   [VcEvaluationController::class, 'form'])->name('vc.evaluations.form');
    Route::post('/appointments/{id}/evaluation',       [VcEvaluationController::class, 'store'])->name('vc.evaluations.store');
    Route::get('/appointments/{id}/evaluation/status', [VcEvaluationController::class, 'status'])->name('vc.evaluations.status');
    Route::post('/evaluations/{id}/review',            [VcEvaluationController::class, 'review'])->name('vc.evaluations.review');

    // Appointment edit requests (post-completion edit approval workflow)
    Route::post('/appointments/{id}/edit-request',           [VcAppointmentEditRequestController::class, 'store'])->name('vc.edit-requests.store');
    Route::get('/appointment-edit-requests',                 [VcAppointmentEditRequestController::class, 'index'])->name('vc.edit-requests.index');
    Route::get('/appointment-edit-requests/list',            [VcAppointmentEditRequestController::class, 'list'])->name('vc.edit-requests.list');
    Route::post('/appointment-edit-requests/{id}/approved',  [VcAppointmentEditRequestController::class, 'approve'])->name('vc.edit-requests.approve');
    Route::post('/appointment-edit-requests/{id}/rejected',  [VcAppointmentEditRequestController::class, 'reject'])->name('vc.edit-requests.reject');

    // Doctor-change requests (preferred-doctor exception approval workflow)
    Route::post('/appointments/{id}/doctor-change-request',  [VcDoctorChangeRequestController::class, 'store'])->name('vc.doctor-change-requests.store');
    Route::get('/doctor-change-requests',                    [VcDoctorChangeRequestController::class, 'index'])->name('vc.doctor-change-requests.index');
    Route::get('/doctor-change-requests/list',               [VcDoctorChangeRequestController::class, 'list'])->name('vc.doctor-change-requests.list');
    Route::post('/doctor-change-requests/{id}/approved',     [VcDoctorChangeRequestController::class, 'approve'])->name('vc.doctor-change-requests.approve');
    Route::post('/doctor-change-requests/{id}/rejected',     [VcDoctorChangeRequestController::class, 'reject'])->name('vc.doctor-change-requests.reject');

    // Role Landing Pages (per-role post-login redirect)
    Route::get('/role-landing',       [VcRoleLandingController::class, 'index'])->name('vc.role-landing.index');
    Route::post('/role-landing/save', [VcRoleLandingController::class, 'save'])->name('vc.role-landing.save');

    // Confidential notes (polymorphic — pet or owner)
    Route::get('/notes/{subject_type}/{subject_id}',  [VcConfidentialNoteController::class, 'index'])->name('vc.notes.index');
    Route::post('/notes/{subject_type}/{subject_id}', [VcConfidentialNoteController::class, 'store'])->name('vc.notes.store');
    Route::post('/notes/{id}/update',                  [VcConfidentialNoteController::class, 'update'])->name('vc.notes.update');
    Route::delete('/notes/{id}',                       [VcConfidentialNoteController::class, 'destroy'])->name('vc.notes.destroy');

    // Controlled-drug register (Phase 1 — flag medicines + log IN/OUT movements)
    Route::get('/controlled-register',        [VcControlledRegisterController::class, 'index'])->name('vc.controlled-register.index');
    Route::get('/controlled-register/list',   [VcControlledRegisterController::class, 'list'])->name('vc.controlled-register.list');
    Route::post('/controlled-register/store', [VcControlledRegisterController::class, 'store'])->name('vc.controlled-register.store');
    Route::get('/controlled-register/print',  [VcControlledRegisterController::class, 'print'])->name('vc.controlled-register.print');
    Route::get('/controlled-register/reconcile',  [VcControlledRegisterController::class, 'reconcile'])->name('vc.controlled-register.reconcile');
    Route::post('/controlled-register/reconcile', [VcControlledRegisterController::class, 'recordReconciliation'])->name('vc.controlled-register.reconcile.store');
    Route::get('/controlled-register/expected-balance', [VcControlledRegisterController::class, 'expectedBalance'])->name('vc.controlled-register.expected-balance');

    // Vet quotation wrapper
    Route::get('/quotations/search',                       [VcQuotationController::class, 'search'])->name('vc.quotations.search');
    Route::get('/appointments/{id}/quotations',            [VcQuotationController::class, 'appointmentList'])->name('vc.quotations.appointment-list');
    Route::post('/appointments/{id}/quotations/link',      [VcQuotationController::class, 'link'])->name('vc.quotations.link');
    Route::delete('/quotations/meta/{id}',                 [VcQuotationController::class, 'unlink'])->name('vc.quotations.unlink');
    Route::post('/quotations/meta/{id}/send-approval',     [VcQuotationController::class, 'sendApproval'])->name('vc.quotations.send-approval');
    Route::post('/quotations/meta/{id}/mark-converted',    [VcQuotationController::class, 'markConverted'])->name('vc.quotations.mark-converted');

    // Wellness plans (P6)
    Route::get('/wellness',                       [VcWellnessController::class, 'index'])->name('vc.wellness.index');
    Route::post('/wellness/plans',                [VcWellnessController::class, 'storePlan'])->name('vc.wellness.store-plan');
    Route::post('/wellness/plans/{id}/benefits',  [VcWellnessController::class, 'addBenefit'])->name('vc.wellness.add-benefit');
    Route::post('/wellness/subscribe',            [VcWellnessController::class, 'subscribe'])->name('vc.wellness.subscribe');
    Route::post('/wellness/subscriptions/{id}/cancel', [VcWellnessController::class, 'cancel'])->name('vc.wellness.cancel');
    Route::get('/wellness/active',                [VcWellnessController::class, 'active'])->name('vc.wellness.active');
    Route::post('/wellness/redeem',               [VcWellnessController::class, 'redeem'])->name('vc.wellness.redeem');

    // Imaging (P5.2)
    Route::get('/imaging',                            [VcImagingController::class, 'index'])->name('vc.imaging.index');
    Route::post('/imaging',                           [VcImagingController::class, 'store'])->name('vc.imaging.store');
    Route::post('/imaging/{id}/status',               [VcImagingController::class, 'updateStatus'])->name('vc.imaging.status');
    Route::post('/imaging/{id}/study',                [VcImagingController::class, 'uploadStudy'])->name('vc.imaging.upload-study');
    Route::delete('/imaging/studies/{id}',            [VcImagingController::class, 'deleteStudy'])->name('vc.imaging.delete-study');
    Route::post('/controlled-register/{id}/resolve', [VcControlledRegisterController::class, 'resolveUnverified'])->name('vc.controlled-register.resolve');
    Route::get('/controlled-register/verify',        [VcControlledRegisterController::class, 'verifyChain'])->name('vc.controlled-register.verify');
    Route::post('/controlled-register/seal',         [VcControlledRegisterController::class, 'sealNow'])->name('vc.controlled-register.seal');

    // Authorised vets (Phase 2 — permit-holders for dispensing controlled drugs)
    Route::get('/authorized-vets',       [VcAuthorizedVetController::class, 'index'])->name('vc.authorized-vets.index');
    Route::post('/authorized-vets',      [VcAuthorizedVetController::class, 'store'])->name('vc.authorized-vets.store');
    Route::delete('/authorized-vets/{id}',[VcAuthorizedVetController::class, 'destroy'])->name('vc.authorized-vets.destroy');

    // Consent Templates (settings) + issued consents
    Route::get('/consent-templates',              [VcConsentTemplateController::class, 'index'])->name('vc.consent-templates.index');
    Route::get('/consent-templates/list',         [VcConsentTemplateController::class, 'list'])->name('vc.consent-templates.list');
    Route::post('/consent-templates/store',       [VcConsentTemplateController::class, 'store'])->name('vc.consent-templates.store');
    Route::get('/consent-templates/{id}/edit',    [VcConsentTemplateController::class, 'edit'])->name('vc.consent-templates.edit');
    Route::post('/consent-templates/{id}/update', [VcConsentTemplateController::class, 'update'])->name('vc.consent-templates.update');
    Route::post('/consent-templates/{id}/clone',  [VcConsentTemplateController::class, 'clone'])->name('vc.consent-templates.clone');
    Route::delete('/consent-templates/{id}',      [VcConsentTemplateController::class, 'destroy'])->name('vc.consent-templates.destroy');

    Route::get('/appointments/{id}/consents',       [VcConsentController::class, 'appointmentList'])->name('vc.consents.appointment-list');
    Route::post('/appointments/{id}/consents/issue',[VcConsentController::class, 'issue'])->name('vc.consents.issue');
    Route::get('/consents/{id}/print',              [VcConsentController::class, 'print'])->name('vc.consents.print');
    Route::get('/consents/{id}/sign',               [VcConsentController::class, 'sign'])->name('vc.consents.sign');
    Route::post('/consents/{id}/sign',              [VcConsentController::class, 'storeSignature'])->name('vc.consents.store-signature');
    Route::post('/consents/{id}/archive',           [VcConsentController::class, 'archive'])->name('vc.consents.archive');
    Route::post('/consents/{id}/signed',            [VcConsentController::class, 'uploadSigned'])->name('vc.consents.upload-signed');
    Route::delete('/consents/{id}/signed',          [VcConsentController::class, 'removeSigned'])->name('vc.consents.remove-signed');
    Route::delete('/consents/{id}',                 [VcConsentController::class, 'destroy'])->name('vc.consents.destroy');

    // Vaccine Types (catalogue management)
    Route::get('/vaccine-types',                    [VcVaccineTypeController::class, 'index'])->name('vc.vaccine-types.index');
    Route::get('/vaccine-types/list',               [VcVaccineTypeController::class, 'list'])->name('vc.vaccine-types.list');
    Route::post('/vaccine-types/store',             [VcVaccineTypeController::class, 'store'])->name('vc.vaccine-types.store');
    Route::get('/vaccine-types/{id}/edit',          [VcVaccineTypeController::class, 'edit'])->name('vc.vaccine-types.edit');
    Route::post('/vaccine-types/{id}/update',       [VcVaccineTypeController::class, 'update'])->name('vc.vaccine-types.update');
    Route::delete('/vaccine-types/{id}',            [VcVaccineTypeController::class, 'destroy'])->name('vc.vaccine-types.destroy');

    // Product picker (links vet catalogue items → ERP products)
    Route::get('/products/search',                  [VcProductPickerController::class, 'search'])->name('vc.products.search');
    Route::get('/products/{id}/lookup',             [VcProductPickerController::class, 'lookup'])->name('vc.products.lookup');

    // Exam not-done reasons (used inside the consultation exam status panel)
    Route::get('/exam-not-done-reasons',            [VcAppointmentController::class, 'examNotDoneReasons'])->name('vc.exam-not-done-reasons.list');

    // Medical Reports
    Route::get('/medical-reports',                  [VcMedicalReportController::class, 'index'])->name('vc.medical-reports.index');
    Route::get('/medical-reports/list',             [VcMedicalReportController::class, 'list'])->name('vc.medical-reports.list');
    Route::get('/medical-reports/create',           [VcMedicalReportController::class, 'create'])->name('vc.medical-reports.create');
    Route::post('/medical-reports/store',           [VcMedicalReportController::class, 'store'])->name('vc.medical-reports.store');
    Route::get('/medical-reports/{id}',             [VcMedicalReportController::class, 'show'])->name('vc.medical-reports.show');
    Route::get('/medical-reports/{id}/edit',        [VcMedicalReportController::class, 'edit'])->name('vc.medical-reports.edit');
    Route::post('/medical-reports/{id}/update',     [VcMedicalReportController::class, 'update'])->name('vc.medical-reports.update');
    Route::delete('/medical-reports/{id}',          [VcMedicalReportController::class, 'destroy'])->name('vc.medical-reports.destroy');

    // Birth & Death Records
    Route::get('/birth-death',                      [VcBirthDeathRecordController::class, 'index'])->name('vc.birth-death.index');
    Route::get('/birth-death/list',                 [VcBirthDeathRecordController::class, 'list'])->name('vc.birth-death.list');
    Route::post('/birth-death/store',               [VcBirthDeathRecordController::class, 'store'])->name('vc.birth-death.store');
    Route::get('/birth-death/{id}/edit',            [VcBirthDeathRecordController::class, 'edit'])->name('vc.birth-death.edit');
    Route::post('/birth-death/{id}/update',         [VcBirthDeathRecordController::class, 'update'])->name('vc.birth-death.update');
    Route::delete('/birth-death/{id}',              [VcBirthDeathRecordController::class, 'destroy'])->name('vc.birth-death.destroy');

    // Reminder Settings & Templates
    Route::get('/reminders/settings',                        [VcReminderController::class, 'settings'])->name('vc.reminders.settings');
    Route::post('/reminders/settings/save',                  [VcReminderController::class, 'saveSettings'])->name('vc.reminders.settings.save');
    Route::get('/reminders/templates',                       [VcReminderController::class, 'templates'])->name('vc.reminders.templates');
    Route::get('/reminders/templates/{id}/edit',             [VcReminderController::class, 'editTemplate'])->name('vc.reminders.templates.edit');
    Route::post('/reminders/templates/{id}/update',          [VcReminderController::class, 'updateTemplate'])->name('vc.reminders.templates.update');
    Route::post('/reminders/send-now',                       [VcReminderController::class, 'sendNow'])->name('vc.reminders.send-now');
    Route::post('/reminders/send-manual/{id}',               [VcReminderController::class, 'sendManual'])->name('vc.reminders.send-manual');
});

// ── AI: voice dictation + clinical NLP (engine = AiCore via VcAiAssistant) ──
// Appointment-screen AI helpers. Migrated off the standalone VetAI module so all
// vet AI now runs on the shared AiCore engine (settings live on ai_providers).
// Route names are kept as vai.* and the prefix as vet-ai so the consultation
// view's existing route('vai.*') references resolve here unchanged.
Route::group([
    'middleware' => ['web', 'auth', 'SetSessionData', 'language', 'timezone', 'AdminSidebarMenu', 'module:VetClinic'],
    'prefix'     => 'vet-ai',
], function () {
    Route::post('/transcribe',               [VcAiController::class, 'transcribe'])->name('vai.transcribe');
    Route::post('/soap-structure',           [VcAiController::class, 'structureSoap'])->name('vai.soap-structure');
    Route::post('/match-chips',              [VcAiController::class, 'matchChips'])->name('vai.match-chips');
    Route::post('/suggest-specialist-exams', [VcAiController::class, 'suggestSpecialistExams'])->name('vai.suggest-specialist-exams');
    Route::post('/extract-symptoms',         [VcAiController::class, 'extractSymptoms'])->name('vai.extract-symptoms');
    Route::post('/cache-advice',             [VcAiController::class, 'cacheAdvice'])->name('vai.cache-advice');
    // Clean recognised text from the browser Web Speech path (which skips
    // /transcribe), so Web Speech dictation gets the same side-talk cleanup.
    Route::post('/clean-transcript',         [VcAiController::class, 'cleanTranscript'])->name('vai.clean-transcript');
});
