<?php

use App\Http\Controllers\Admin\AiIntegrationController;
use App\Http\Controllers\Admin\CrmController;
use App\Http\Controllers\Admin\InventoryController;
use App\Http\Controllers\Admin\MasterplanDigitizerController;
use App\Http\Controllers\Admin\ProjectController;
use App\Http\Controllers\Admin\QuoteController;
use App\Http\Controllers\Admin\ReservationController;
use App\Http\Controllers\Admin\SalesCenterController;
use App\Http\Controllers\Admin\SystemUpdateController;
use App\Http\Controllers\Install\InstallerController;
use App\Http\Controllers\PortalExplorerController;
use App\Http\Controllers\PortalHomeController;
use App\Http\Controllers\PortalLeadController;
use App\Http\Controllers\PortalProjectController;
use App\Http\Controllers\PublicQuoteController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::middleware('guest.installer')->group(function () {
    Route::get('instalar', [InstallerController::class, 'show'])->name('install.show');
    Route::post('instalar', [InstallerController::class, 'store'])
        ->middleware('throttle:10,1')
        ->name('install.store');
    Route::get('instalar/listo', [InstallerController::class, 'done'])->name('install.done');
});

Route::get('/', PortalHomeController::class)->name('home');
Route::redirect('portal', '/', 301)->name('portal.legacy');
Route::redirect('portal/explorador-3d', '/explorador-3d', 301);
Route::get('explorador-3d', PortalExplorerController::class)->name('portal.explorer');
Route::get('proyecto/{slug}', PortalProjectController::class)->name('portal.project');
Route::get('cotizacion/{token}', PublicQuoteController::class)->name('portal.quote');
Route::post('interes', [PortalLeadController::class, 'store'])
    ->middleware('throttle:8,1')
    ->name('portal.lead');
Route::redirect('estudio-3d', '/admin/digitalizador', 301)->name('studio.legacy');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('dashboard', function (Request $request) {
        return redirect()->route($request->user()->isAdmin() ? 'admin.dashboard' : 'client.dashboard');
    })->name('dashboard');

    Route::inertia('mi-cuenta', 'client/dashboard')->name('client.dashboard');

    Route::middleware('role:admin')->prefix('admin')->name('admin.')->group(function () {
        Route::get('/', SalesCenterController::class)->name('dashboard');

        Route::get('proyectos', [ProjectController::class, 'index'])->name('projects');
        Route::get('proyectos/{development:slug}', [ProjectController::class, 'show'])->name('projects.show');
        Route::put('proyectos/{development:slug}', [ProjectController::class, 'update'])->name('projects.update');
        Route::put('proyectos/{development:slug}/lotes/{lot}', [ProjectController::class, 'updateLot'])
            ->whereNumber('lot')
            ->name('projects.lot');
        Route::put('proyectos/{development:slug}/precios', [ProjectController::class, 'priceLots'])
            ->name('projects.pricing');
        Route::post('proyectos/{development:slug}/galeria', [ProjectController::class, 'storeMedia'])
            ->name('projects.media.store');
        Route::put('proyectos/{development:slug}/galeria', [ProjectController::class, 'reorderMedia'])
            ->name('projects.media.reorder');
        Route::put('proyectos/{development:slug}/galeria/{medium}/portada', [ProjectController::class, 'setCover'])
            ->whereNumber('medium')
            ->name('projects.media.cover');
        Route::delete('proyectos/{development:slug}/galeria/{medium}', [ProjectController::class, 'destroyMedia'])
            ->whereNumber('medium')
            ->name('projects.media.destroy');
        Route::delete('proyectos/{development:slug}', [ProjectController::class, 'destroy'])->name('projects.destroy');

        Route::get('inventario', [InventoryController::class, 'index'])->name('inventory');
        Route::get('inventario/exportar', [InventoryController::class, 'export'])
            ->middleware('throttle:10,1')
            ->name('inventory.export');

        Route::post('apartados', [ReservationController::class, 'store'])->name('reservations.store');
        Route::put('apartados/{reservation}', [ReservationController::class, 'update'])->name('reservations.update');
        Route::post('cotizaciones', [QuoteController::class, 'store'])->name('quotes.store');
        Route::delete('cotizaciones/{quote}', [QuoteController::class, 'destroy'])->name('quotes.destroy');

        Route::get('crm', [CrmController::class, 'index'])->name('crm');
        Route::post('crm', [CrmController::class, 'store'])->name('crm.store');
        Route::put('crm/{prospect}', [CrmController::class, 'update'])->name('crm.update');
        Route::delete('crm/{prospect}', [CrmController::class, 'destroy'])->name('crm.destroy');
        Route::post('crm/{prospect}/interacciones', [CrmController::class, 'logInteraction'])
            ->name('crm.interaction');

        Route::get('digitalizador', [MasterplanDigitizerController::class, 'show'])->name('studio');
        Route::post('digitalizador/analizar', [MasterplanDigitizerController::class, 'inspect'])
            ->middleware('throttle:30,1')
            ->name('digitalizer.inspect');
        Route::post('digitalizador/digitalizar', [MasterplanDigitizerController::class, 'digitize'])
            ->middleware('throttle:20,1')
            ->name('digitalizer.digitize');
        Route::patch('digitalizador/lote/{lot}', [MasterplanDigitizerController::class, 'updateLot'])
            ->whereNumber('lot')
            ->name('digitalizer.lot');

        Route::get('actualizacion', [SystemUpdateController::class, 'show'])->name('system.update');
        Route::post('actualizacion', [SystemUpdateController::class, 'store'])
            ->middleware('throttle:3,10')
            ->name('system.update.store');

        Route::get('configuracion/ia', [AiIntegrationController::class, 'show'])->name('ai.show');
        Route::put('configuracion/ia', [AiIntegrationController::class, 'update'])->name('ai.update');
        Route::post('configuracion/ia/probar', [AiIntegrationController::class, 'test'])
            ->middleware('throttle:5,1')
            ->name('ai.test');
    });
});

require __DIR__.'/settings.php';
