<?php

use App\Http\Controllers\ClientController;
use App\Http\Controllers\ClinicHubController;
use App\Http\Controllers\MedicalRecordController;
use App\Http\Controllers\PatientController;
use App\Http\Controllers\PatientMediaController;
use App\Http\Controllers\PosController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProductReservationController;
use App\Http\Controllers\AppointmentController;
use App\Http\Controllers\MedicoController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::permanentRedirect('/products', '/farmacia/products');
    Route::permanentRedirect('/pos', '/farmacia/pos');

    Route::prefix('farmacia')->group(function () {
        Route::post('products/{product}/stock', [ProductController::class, 'adjustStock'])
            ->name('products.stock');

        Route::resource('products', ProductController::class)
            ->names([
                'index' => 'products.index',
                'create' => 'products.create',
                'store' => 'products.store',
                'show' => 'products.show',
                'edit' => 'products.edit',
                'update' => 'products.update',
                'destroy' => 'products.destroy',
            ]);

        Route::prefix('pos')->name('pos.')->group(function () {
            Route::get('/', [PosController::class, 'index'])->name('index');
            Route::post('add', [PosController::class, 'addLine'])->name('add');
            Route::post('line', [PosController::class, 'updateLine'])->name('line.update');
            Route::post('remove', [PosController::class, 'removeLine'])->name('line.remove');
            Route::post('clear', [PosController::class, 'clearCart'])->name('clear');
            Route::post('client', [PosController::class, 'setClient'])->name('client.set');
            Route::delete('client', [PosController::class, 'clearClient'])->name('client.clear');
            Route::post('discount', [PosController::class, 'setDiscount'])->name('discount.set');
            Route::delete('discount', [PosController::class, 'clearDiscount'])->name('discount.clear');
            Route::post('checkout', [PosController::class, 'checkout'])->name('checkout');
            Route::post('from-reservation/{reservation}', [PosController::class, 'loadFromReservation'])
                ->name('from-reservation');
            Route::get('ticket/{sale}', [PosController::class, 'show'])->name('ticket');
        });
    });

    Route::prefix('clinica')->name('clinica.')->group(function () {
        Route::get('expediente', [ClinicHubController::class, 'index'])->name('hub');
        Route::get('expediente/buscar', [ClinicHubController::class, 'search'])->name('search');

        Route::get('citas/{appointment}/imprimir', [AppointmentController::class, 'print'])
            ->name('appointments.print');
        Route::resource('citas', AppointmentController::class)
            ->parameters(['citas' => 'appointment'])
            ->names([
                'index' => 'appointments.index',
                'create' => 'appointments.create',
                'store' => 'appointments.store',
                'show' => 'appointments.show',
                'edit' => 'appointments.edit',
                'update' => 'appointments.update',
                'destroy' => 'appointments.destroy',
            ]);

        Route::resource('medicos', MedicoController::class)
            ->only(['index', 'create', 'store', 'edit', 'update', 'destroy']);
    });

    Route::resource('clients', ClientController::class);
    Route::resource('clients.patients', PatientController::class)
        ->scoped()
        ->only(['create', 'store', 'show', 'edit', 'update', 'destroy']);

    Route::prefix('clients/{client}/patients/{patient}')
        ->name('clients.patients.')
        ->group(function () {
            Route::get('medical-records/{medical_record}/imprimir', [MedicalRecordController::class, 'print'])
                ->name('medical-records.print');
            Route::resource('medical-records', MedicalRecordController::class);
            Route::get('product-reservations/{product_reservation}/line-items', [ProductReservationController::class, 'lineItems'])
                ->name('product-reservations.line-items');
            Route::resource('product-reservations', ProductReservationController::class)
                ->only(['index', 'create', 'store', 'show', 'destroy']);
            Route::post('media', [PatientMediaController::class, 'store'])->name('media.store');
            Route::delete('media/{patientMedia}', [PatientMediaController::class, 'destroy'])->name('media.destroy');
        });
});

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
