<?php

use App\Http\Controllers\Public\PublicBookingController;
use App\Http\Controllers\Tenant\AppointmentController;
use App\Http\Controllers\Tenant\AvailabilityController;
use App\Http\Controllers\Tenant\BillingController;
use App\Http\Controllers\Tenant\DashboardController;
use App\Http\Controllers\Tenant\DentistController;
use App\Http\Controllers\Tenant\PatientController;
use App\Http\Controllers\Tenant\ScheduleExceptionController;
use Illuminate\Support\Facades\Route;

/*
| Runs on {clinic}.central-domain with ['web','tenant'] middleware.
*/

// ---- Public booking page (no auth, throttled) ----
Route::prefix('book')->name('public.booking.')->group(function () {
    Route::get('/', [PublicBookingController::class, 'index'])->name('index');
    Route::get('/slots', [PublicBookingController::class, 'slots'])
        ->middleware('throttle:30,1')->name('slots');
    Route::post('/', [PublicBookingController::class, 'store'])
        ->middleware('throttle:10,1')->name('store');
    Route::get('/confirmed/{uuid}', [PublicBookingController::class, 'confirmed'])->name('confirmed');
    Route::post('/cancel/{uuid}', [PublicBookingController::class, 'cancel'])
        ->middleware('throttle:10,1')->name('cancel');
});

// ---- Public review gateway (signed link from WhatsApp) ----
Route::get('/review/{appointment}', [\App\Http\Controllers\Public\ReviewController::class, 'show'])->name('public.review');
Route::post('/review/{appointment}', [\App\Http\Controllers\Public\ReviewController::class, 'submit'])->name('public.review.submit');

// ---- Authenticated clinic dashboard ----
Route::middleware(['auth', 'subscribed'])->name('tenant.')->group(function () {
    Route::get('/', fn () => redirect()->route('tenant.dashboard'));
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/billing', [BillingController::class, 'index'])->name('billing');
    Route::post('/billing/proof', [BillingController::class, 'submitProof'])->name('billing.proof');
    Route::get('/billing/proof/{payment}', [BillingController::class, 'proof'])->name('billing.proof.show');
    Route::get('/notifications', [\App\Http\Controllers\Tenant\NotificationController::class, 'index'])->name('notifications');

    // Dentists + weekly schedules (edited inside the dentist form)
    Route::resource('dentists', DentistController::class)->except(['show']);

    // Schedule exceptions (days off / extra hours)
    Route::get('schedule-exceptions', [ScheduleExceptionController::class, 'index'])->name('exceptions.index');
    Route::post('schedule-exceptions', [ScheduleExceptionController::class, 'store'])->name('exceptions.store');
    Route::delete('schedule-exceptions/{exception}', [ScheduleExceptionController::class, 'destroy'])->name('exceptions.destroy');

    // Patients CRM
    Route::resource('patients', PatientController::class);

    // Appointments + calendar
    Route::get('appointments', [AppointmentController::class, 'index'])->name('appointments.index');
    Route::get('appointments/feed', [AppointmentController::class, 'feed'])->name('appointments.feed');
    Route::post('appointments', [AppointmentController::class, 'store'])->name('appointments.store');
    Route::patch('appointments/{appointment}', [AppointmentController::class, 'update'])->name('appointments.update');
    Route::patch('appointments/{appointment}/status', [AppointmentController::class, 'updateStatus'])->name('appointments.status');

    Route::get('availability', AvailabilityController::class)->name('availability');

    // ---- M3: Dental chart (centerpiece) ----
    Route::get('patients/{patient}/chart', [\App\Http\Controllers\Tenant\ToothChartController::class, 'show'])->name('patients.chart');
    Route::get('patients/{patient}/chart/data', [\App\Http\Controllers\Tenant\ToothChartController::class, 'data'])->name('patients.chart.data');
    Route::post('patients/{patient}/chart', [\App\Http\Controllers\Tenant\ToothChartController::class, 'store'])->name('patients.chart.store');
    Route::delete('patients/{patient}/chart/{record}', [\App\Http\Controllers\Tenant\ToothChartController::class, 'destroy'])->name('patients.chart.destroy');

    // ---- M3: Procedures catalog ----
    Route::get('procedures', [\App\Http\Controllers\Tenant\ProcedureController::class, 'index'])->name('procedures.index');
    Route::post('procedures', [\App\Http\Controllers\Tenant\ProcedureController::class, 'store'])->name('procedures.store');
    Route::put('procedures/{procedure}', [\App\Http\Controllers\Tenant\ProcedureController::class, 'update'])->name('procedures.update');
    Route::delete('procedures/{procedure}', [\App\Http\Controllers\Tenant\ProcedureController::class, 'destroy'])->name('procedures.destroy');

    // ---- M3: Treatment plans ----
    Route::get('treatment-plans', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'index'])->name('treatment-plans.index');
    Route::post('treatment-plans', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'store'])->name('treatment-plans.store');
    Route::get('treatment-plans/{treatment_plan}', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'show'])->name('treatment-plans.show');
    Route::patch('treatment-plans/{treatment_plan}', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'update'])->name('treatment-plans.update');
    Route::patch('treatment-plans/{treatment_plan}/status', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'updateStatus'])->name('treatment-plans.status');
    Route::post('treatment-plans/{treatment_plan}/items', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'addItem'])->name('treatment-plans.items.store');
    Route::post('treatment-plans/{treatment_plan}/items/{item}/complete', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'completeItem'])->name('treatment-plans.items.complete');
    Route::post('treatment-plans/{treatment_plan}/items/{item}/cancel', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'cancelItem'])->name('treatment-plans.items.cancel');
    Route::get('treatment-plans/{treatment_plan}/pdf', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'pdf'])->name('treatment-plans.pdf');

    // ---- M3: Patient files (photos & X-rays) ----
    Route::get('patients/{patient}/files', [\App\Http\Controllers\Tenant\PatientFileController::class, 'index'])->name('patients.files');
    Route::post('patients/{patient}/files', [\App\Http\Controllers\Tenant\PatientFileController::class, 'store'])->name('patients.files.store');
    Route::post('patients/{patient}/files/{before}/pair/{after}', [\App\Http\Controllers\Tenant\PatientFileController::class, 'pair'])->name('patients.files.pair');
    Route::delete('patients/{patient}/files/{file}', [\App\Http\Controllers\Tenant\PatientFileController::class, 'destroy'])->name('patients.files.destroy');
    Route::get('files/{file}/{variant?}', [\App\Http\Controllers\Tenant\PatientFileController::class, 'serve'])->name('files.serve');

    // ---- M4: Invoices, payments, installments, reports ----
    Route::get('invoices', [\App\Http\Controllers\Tenant\InvoiceController::class, 'index'])->name('invoices.index');
    Route::post('invoices', [\App\Http\Controllers\Tenant\InvoiceController::class, 'storeCustom'])->name('invoices.store');
    Route::post('treatment-plans/{treatment_plan}/invoice', [\App\Http\Controllers\Tenant\InvoiceController::class, 'storeFromPlan'])->name('treatment-plans.invoice');
    Route::get('invoices/{invoice}', [\App\Http\Controllers\Tenant\InvoiceController::class, 'show'])->name('invoices.show');
    Route::post('invoices/{invoice}/issue', [\App\Http\Controllers\Tenant\InvoiceController::class, 'issue'])->name('invoices.issue');
    Route::post('invoices/{invoice}/void', [\App\Http\Controllers\Tenant\InvoiceController::class, 'void'])->name('invoices.void');
    Route::get('invoices/{invoice}/pdf', [\App\Http\Controllers\Tenant\InvoiceController::class, 'pdf'])->name('invoices.pdf');
    Route::post('invoices/{invoice}/payments', [\App\Http\Controllers\Tenant\PaymentController::class, 'store'])->name('invoices.payments.store');
    Route::post('invoices/{invoice}/installments', [\App\Http\Controllers\Tenant\InstallmentController::class, 'store'])->name('invoices.installments.store');
    Route::get('installments', [\App\Http\Controllers\Tenant\InstallmentController::class, 'index'])->name('installments.index');
    Route::get('reports/revenue', [\App\Http\Controllers\Tenant\RevenueReportController::class, 'index'])->name('reports.revenue');

    // ---- M5: WhatsApp ----
    Route::get('whatsapp', [\App\Http\Controllers\Tenant\WhatsAppController::class, 'index'])->name('whatsapp.index');
    Route::get('patients/{patient}/whatsapp', [\App\Http\Controllers\Tenant\WhatsAppController::class, 'thread'])->name('patients.whatsapp');
    Route::post('patients/{patient}/whatsapp', [\App\Http\Controllers\Tenant\WhatsAppController::class, 'send'])->name('patients.whatsapp.send');
    Route::post('whatsapp/{message}/retry', [\App\Http\Controllers\Tenant\WhatsAppController::class, 'retry'])->name('whatsapp.retry');
    Route::get('whatsapp-templates', [\App\Http\Controllers\Tenant\WaTemplateController::class, 'index'])->name('whatsapp.templates');
    Route::put('whatsapp-templates/{key}', [\App\Http\Controllers\Tenant\WaTemplateController::class, 'update'])->name('whatsapp.templates.update');

    // ---- M5: Recalls ----
    Route::get('recalls', [\App\Http\Controllers\Tenant\RecallController::class, 'index'])->name('recalls.index');
    Route::post('recalls', [\App\Http\Controllers\Tenant\RecallController::class, 'store'])->name('recalls.store');
    Route::patch('recalls/{recall}', [\App\Http\Controllers\Tenant\RecallController::class, 'update'])->name('recalls.update');

    // ---- M5: PDF → WhatsApp ----
    Route::post('treatment-plans/{treatment_plan}/send-pdf', [\App\Http\Controllers\Tenant\TreatmentPlanController::class, 'sendPdf'])->name('treatment-plans.send-pdf');
    Route::post('invoices/{invoice}/send-whatsapp', [\App\Http\Controllers\Tenant\InvoiceController::class, 'sendWhatsApp'])->name('invoices.send-whatsapp');

    // ---- M3: Clinical timeline ----
    Route::get('patients/{patient}/timeline', [\App\Http\Controllers\Tenant\TimelineController::class, 'show'])->name('patients.timeline');
});
