<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\InstallerController;
use App\Http\Controllers\StoreController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\BrandController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\PageBuilderController;

/*
|--------------------------------------------------------------------------
| DOMINIO 1: INSTALADOR
|--------------------------------------------------------------------------
| Siempre registrado. El middleware RedirectIfNotInstalled decide si el
| sitio está instalado (según el marcador storage/app/installed).
*/
Route::get('/install', [InstallerController::class, 'index'])->name('installer.index');
Route::post('/install', [InstallerController::class, 'process'])->name('installer.process');
Route::get('/install/success', [InstallerController::class, 'success'])->name('installer.success');

/*
|--------------------------------------------------------------------------
| DOMINIO 2: TIENDA PÚBLICA
|--------------------------------------------------------------------------
*/
Route::get('/', [StoreController::class, 'renderHome'])->name('store.home');
Route::get('/pagina/{slug}', [StoreController::class, 'renderPage'])->name('store.page');
Route::get('/producto/{slug}', [StoreController::class, 'showProduct'])->name('store.product');

Route::post('/cart/add', [CartController::class, 'add'])->name('cart.add');
Route::post('/cart/update', [CartController::class, 'update'])->name('cart.update');
Route::post('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');
Route::get('/carrito', [CartController::class, 'index'])->name('cart.index');

Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::post('/checkout/process', [CheckoutController::class, 'process'])->name('checkout.process');
Route::get('/checkout/success', [CheckoutController::class, 'success'])->name('checkout.success');
Route::get('/checkout/cancel', [CheckoutController::class, 'cancel'])->name('checkout.cancel');

/*
|--------------------------------------------------------------------------
| DOMINIO 3: PANEL DE ADMINISTRACIÓN
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin'])->group(function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');

    Route::get('products/plantilla', [ProductController::class, 'downloadTemplate'])->name('products.template');
    Route::post('products/import', [ProductController::class, 'import'])->name('products.import');

    Route::resource('products', ProductController::class);
    Route::resource('categories', CategoryController::class);
    Route::resource('brands', BrandController::class);

    Route::get('settings', [SettingController::class, 'edit'])->name('settings.edit');
    Route::put('settings', [SettingController::class, 'update'])->name('settings.update');

    Route::get('pages', [PageBuilderController::class, 'index'])->name('pages.index');
    Route::get('pages/create', [PageBuilderController::class, 'create'])->name('pages.create');
    Route::get('pages/{page}/builder', [PageBuilderController::class, 'builder'])->name('pages.builder');
    Route::post('pages', [PageBuilderController::class, 'store'])->name('pages.store');
    Route::put('pages/{page}/layout', [PageBuilderController::class, 'saveLayout'])->name('pages.layout');
    Route::delete('pages/{page}', [PageBuilderController::class, 'destroy'])->name('pages.destroy');
});
