<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\SaleController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\InvestorReportController;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PublicCatalogController;
use App\Http\Controllers\AdminReservationController;
use App\Http\Controllers\CashReconciliationController;
use App\Http\Controllers\PromotionController;
use App\Http\Controllers\CreditController;
use App\Http\Controllers\DebtController;
use App\Http\Controllers\AuditController;

Route::get('/', [PublicCatalogController::class, 'index'])->name('catalog.index');
Route::get('/dni-lookup', [PublicCatalogController::class, 'lookupDni'])->name('dni.lookup');
Route::post('/reserve', [PublicCatalogController::class, 'reserve'])->name('reserve.store');
Route::get('/my-history', [PublicCatalogController::class, 'history'])->name('history.index');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [ReportController::class, 'dashboard'])->name('dashboard');
    Route::get('/dashboard/stats', [ReportController::class, 'stats'])->name('dashboard.stats');
    
    // Products, Categories, Suppliers CRUD
    Route::middleware('permission:gestionar_productos')->group(function() {
        Route::resource('products', ProductController::class);
        Route::resource('suppliers', SupplierController::class);
        Route::resource('categories', \App\Http\Controllers\CategoryController::class);
    });
    
    // Clients CRUD
    Route::resource('clients', \App\Http\Controllers\ClientController::class)->middleware('permission:ver_clientes');

    // Users Management CRUD
    Route::resource('users', UserController::class)->middleware('permission:gestionar_usuarios');

    // Gestión de Roles y Permisos
    Route::resource('roles', \App\Http\Controllers\RoleController::class)->middleware('permission:gestionar_roles');

    // Settings
    Route::get('/settings', [\App\Http\Controllers\SettingController::class, 'index'])->name('settings.index')->middleware('permission:ver_configuracion');
    Route::post('/settings', [\App\Http\Controllers\SettingController::class, 'update'])->name('settings.update')->middleware('permission:ver_configuracion');

    // Expenses CRUD
    Route::resource('expenses', ExpenseController::class)->except(['show', 'update', 'edit'])->middleware('permission:gestionar_cajas');

    // POS and Sales History
    Route::middleware('permission:ver_pos')->group(function() {
        Route::get('/pos', [SaleController::class, 'create'])->name('sales.create');
        Route::post('/pos', [SaleController::class, 'store'])->name('sales.store');
        Route::get('/sales', [SaleController::class, 'index'])->name('sales.index');
        Route::get('/sales/{sale}/ticket', [SaleController::class, 'ticket'])->name('sales.ticket');

        // POS AJAX endpoints
        Route::get('/pos/lookup', [SaleController::class, 'lookup'])->name('sales.lookup');
        Route::get('/pos/products', [SaleController::class, 'products'])->name('sales.products');
        Route::get('/pos/held', [SaleController::class, 'getHeldSales'])->name('sales.held.index');
        Route::post('/pos/held', [SaleController::class, 'storeHeldSale'])->name('sales.held.store');
        Route::put('/pos/held/{heldSale}', [SaleController::class, 'updateHeldSale'])->name('sales.held.update');
        Route::delete('/pos/held/{heldSale}', [SaleController::class, 'deleteHeldSale'])->name('sales.held.destroy');
    });

    // Reservations Admin
    Route::middleware('permission:ver_pos')->group(function() {
        Route::get('/reservations', [AdminReservationController::class, 'index'])->name('reservations.index');
        Route::post('/reservations/{reservation}/confirm', [AdminReservationController::class, 'confirm'])->name('reservations.confirm');
        Route::post('/reservations/{reservation}/cancel', [AdminReservationController::class, 'cancel'])->name('reservations.cancel');
    });

    // Promotions CRUD
    Route::resource('promotions', PromotionController::class)->middleware('permission:gestionar_productos');

    // CXC: Cuentas por Cobrar (Créditos Clientes)
    Route::middleware('permission:ver_clientes')->group(function() {
        Route::get('/credits', [CreditController::class, 'index'])->name('credits.index');
        Route::get('/credits/{client}', [CreditController::class, 'show'])->name('credits.show');
        Route::post('/credits/{client}/payment', [CreditController::class, 'storePayment'])->name('credits.payment.store');
    });

    // CXP: Cuentas por Pagar (Deudas Negocio)
    Route::middleware('permission:gestionar_cajas')->group(function() {
        Route::resource('debts', DebtController::class);
        Route::get('/debts/{debt}/installments', [DebtController::class, 'installments'])->name('debts.installments');
        Route::post('/installments/{installment}/pay', [DebtController::class, 'payInstallment'])->name('debts.installments.pay');
    });

    // Investor Reports
    Route::middleware('permission:ver_reportes_inversores')->group(function() {
        Route::get('/investor-reports', [InvestorReportController::class, 'index'])->name('investors.dashboard');
        Route::post('/investor-reports/inject', [InvestorReportController::class, 'storeInjection'])->name('investors.inject');
        Route::post('/investor-reports/settle', [InvestorReportController::class, 'storeSettlement'])->name('investors.store-settlement');
        Route::post('/investor-reports/confirm-settlement/{settlement}', [InvestorReportController::class, 'confirmSettlement'])->name('investors.confirm-settlement');
    });

    // Control Interno: Auditoría de Sistema
    Route::get('/audit-logs', [AuditController::class, 'index'])->name('admin.audit_logs.index')->middleware('permission:gestionar_usuarios');
    Route::get('/audit-logs/{auditLog}', [AuditController::class, 'show'])->name('admin.audit_logs.show')->middleware('permission:gestionar_usuarios');

    // Gestión de Caja (Arqueo)
    Route::middleware('permission:gestionar_cajas')->prefix('cash')->name('cash.')->group(function () {
        Route::get('/', [CashReconciliationController::class, 'index'])->name('index');
        Route::post('/open', [CashReconciliationController::class, 'storeOpen'])->name('storeOpen');
        Route::get('/{cashReconciliation}/close', [CashReconciliationController::class, 'closeForm'])->name('closeForm');
        Route::post('/{cashReconciliation}/close', [CashReconciliationController::class, 'storeClose'])->name('storeClose');
    });
});

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';
