<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\CatalogoController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\ClientController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\ProductosController;
use App\Http\Controllers\RegisterController;
use App\Http\Controllers\TallerController;
use App\Http\Controllers\AgendaController;
use App\Http\Controllers\AppointmentController;
use App\Http\Controllers\SupplierController;
use App\Models\Product;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

Route::get('/',HomeController::class);

// Route::get('/catalogo', [CatalogoController::class,'catalogo'])->name('catalogo');
Route::resource('catalogo', CatalogoController::class)->only(['index', 'show'])->parameters(['catalogo' => 'product']);

Route::get('login', function(){ return view('login'); })->name('login');
Route::post('login', LoginController::class)->middleware('throttle:5,1')->name('login.attempt');

Route::view('register', 'register')->name('register');
Route::post('register', RegisterController::class)->name('register.store');

Route::post('logout', function(){
    Auth::guard('web')->logout();
    
    Session::invalidate();
    Session::regenerateToken();
    
    return redirect('/');
})->name('logout');

Route::middleware(['auth'])->group(function () {
    Route::view('dashboard', 'dashboard')->name('dashboard');
    Route::resource('clients', ClientController::class);
    Route::get('suppliers/{supplier}/order', [SupplierController::class, 'order'])->name('suppliers.order');
    Route::resource('suppliers', SupplierController::class);
    Route::resource('productos', ProductosController::class)->parameters([
        'productos' => 'product' 
    ]);
    Route::resource('sales', SaleController::class)->only(['index', 'create', 'store', 'show']);
    // Rutas de Taller
    Route::controller(TallerController::class)->prefix('taller')->name('taller.')->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/create', 'create')->name('create');
        Route::post('/', 'store')->name('store');
        Route::patch('/{appointment}/status', 'updateStatus')->name('updateStatus');
    });
    Route::get('/agenda', [AgendaController::class, 'index'])->name('agenda');
    Route::get('/appointments/{appointment}', [AppointmentController::class, 'show'])
    ->name('appointments.show');
    Route::view('/faq', 'faq')->name('faq');
});
