<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RbacController;
use App\Http\Controllers\ActivityLogController;
use App\Http\Controllers\MenuController;
use App\Http\Controllers\OutletController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\IngredientController;
use App\Http\Controllers\PosController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SupplierController;
use App\Http\Controllers\TableController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\ReservationController;
use App\Http\Controllers\PromoController;
use App\Http\Controllers\KdsController;
use App\Http\Controllers\OpnameController;
use App\Http\Controllers\WasteController;
use App\Http\Controllers\LicenseController;
use App\Http\Controllers\CourierController;
use App\Http\Controllers\VoucherController;
use App\Http\Controllers\PurchaseOrderController;
use App\Http\Controllers\QzTrayController;
use App\Http\Controllers\ModifierController;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::prefix('admin')->group(function () {
    Route::get('/license/activate', [LicenseController::class, 'showActivationPage'])->name('license.activate');
    Route::post('/license/activate', [LicenseController::class, 'activate'])->name('license.activate.post');

    Route::middleware(['license.verify'])->group(function () {
        Route::get('/', function () {
            return redirect()->route('dashboard');
        });

    Route::get('/dashboard', function (\Illuminate\Http\Request $request) {
        if ($request->user()->hasRole('Kasir')) {
            return redirect()->route('pos.index');
        }
        if ($request->user()->hasRole('Kitchen')) {
            return redirect()->route('kds.index');
        }
        return app(DashboardController::class)->index($request);
    })->middleware(['auth', 'verified'])->name('dashboard');

    Route::get('/guide', function () {
        return Inertia::render('CafePOS/Guide/Index');
    })->middleware(['auth', 'verified'])->name('guide.index');

    Route::get('/dashboard/print', function (\Illuminate\Http\Request $request) {
        if ($request->user()->hasRole('Kasir')) {
            return redirect()->route('pos.index');
        }
        if ($request->user()->hasRole('Kitchen')) {
            return redirect()->route('kds.index');
        }
        return app(DashboardController::class)->print($request);
    })->middleware(['auth', 'verified'])->name('dashboard.print');

    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');
        Route::post('/qz-tray/sign', [QzTrayController::class, 'sign'])->name('qz-tray.sign');
    });

    // Settings & Menus module routes
    Route::middleware(['auth', 'permission:manage settings'])->group(function () {
        Route::get('/settings', [SettingController::class, 'edit'])->name('settings.edit');
        Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');

        // QZ Tray settings endpoints
        Route::get('/settings/qz-tray', [QzTrayController::class, 'index'])->name('settings.qz-tray.index');
        Route::post('/settings/qz-tray/generate', [QzTrayController::class, 'generate'])->name('settings.qz-tray.generate');
        Route::post('/settings/qz-tray/regenerate', [QzTrayController::class, 'regenerate'])->name('settings.qz-tray.regenerate');
        Route::delete('/settings/qz-tray', [QzTrayController::class, 'destroy'])->name('settings.qz-tray.destroy');
        Route::get('/settings/qz-tray/download', [QzTrayController::class, 'download'])->name('settings.qz-tray.download');

        Route::post('/menus/groups/rename', [MenuController::class, 'renameGroup'])->name('menus.groups.rename');
        Route::post('/menus/groups/delete', [MenuController::class, 'deleteGroup'])->name('menus.groups.delete');
        Route::resource('menus', MenuController::class)->except(['create', 'show', 'edit']);
    });

    // User Management & RBAC routes
    Route::middleware(['auth', 'permission:manage users'])->group(function () {
        Route::get('/users', [UserController::class, 'index'])->name('users.index');
        Route::post('/users', [UserController::class, 'store'])->name('users.store');
        Route::put('/users/{user}', [UserController::class, 'update'])->name('users.update');
        Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');

        Route::get('/rbac', [RbacController::class, 'index'])->name('rbac.index');
        Route::post('/rbac', [RbacController::class, 'store'])->name('rbac.store');
        Route::put('/rbac/{role}', [RbacController::class, 'update'])->name('rbac.update');
        Route::delete('/rbac/{role}', [RbacController::class, 'destroy'])->name('rbac.destroy');

        // Permission CRUD
        Route::post('/rbac/permissions', [RbacController::class, 'storePermission'])->name('rbac.permissions.store');
        Route::put('/rbac/permissions/{permission}', [RbacController::class, 'updatePermission'])->name('rbac.permissions.update');
        Route::delete('/rbac/permissions/{permission}', [RbacController::class, 'destroyPermission'])->name('rbac.permissions.destroy');
    });

    // Activity logs route
    Route::middleware(['auth', 'permission:view logs'])->group(function () {
        Route::get('/logs', [ActivityLogController::class, 'index'])->name('logs.index');
    });

    // CafePOS Cashier & Transactions Routes
    Route::middleware(['auth'])->group(function () {
        // POS terminal
        Route::get('/pos', [PosController::class, 'index'])->name('pos.index');
        Route::post('/pos/checkout', [PosController::class, 'checkout'])->name('pos.checkout');

        // History
        Route::get('/transactions', [PosController::class, 'transactions'])->name('transactions.index');
        Route::post('/transactions/{transaction}/confirm-payment', [PosController::class, 'confirmPayment'])->name('transactions.confirm-payment');
        Route::post('/transactions/{transaction}/void', [PosController::class, 'void'])->name('transactions.void');
        Route::post('/transactions/{transaction}/delivery-status', [PosController::class, 'updateDeliveryStatus'])->name('transactions.delivery-status');
        Route::delete('/transactions/{transaction}', [PosController::class, 'destroy'])->name('transactions.destroy');
        Route::get('/transactions/{transaction}/print', [PosController::class, 'printReceipt'])->name('transactions.print');
        Route::get('/transactions/{transaction}/kitchen-print', [PosController::class, 'printKitchenTicket'])->name('transactions.kitchen-print');

        // Kitchen Display System
        Route::get('/kds', [KdsController::class, 'index'])->name('kds.index');
        Route::post('/kds/items/{item}/status', [KdsController::class, 'updateStatus'])->name('kds.items.status');

        // Tables, Customers, Reservations, Promos
        Route::resource('tables', TableController::class)->except(['create', 'show', 'edit']);
        Route::post('/tables/merge', [TableController::class, 'mergeTables'])->name('tables.merge');
        Route::post('/tables/split', [TableController::class, 'splitTable'])->name('tables.split');
        Route::resource('customers', CustomerController::class)->except(['create', 'show', 'edit']);
        Route::resource('reservations', ReservationController::class)->except(['create', 'show', 'edit']);
        Route::resource('promos', PromoController::class)->except(['create', 'show', 'edit']);
        Route::resource('couriers', CourierController::class)->except(['create', 'show', 'edit']);
        Route::resource('vouchers', VoucherController::class)->except(['create', 'show', 'edit']);
        Route::post('/vouchers/verify', [VoucherController::class, 'verify'])->name('vouchers.verify');
        
        // Purchase Orders (PO / GRN)
        Route::resource('purchase-orders', PurchaseOrderController::class)->except(['create', 'show', 'edit']);
        Route::post('/purchase-orders/{purchase_order}/send', [PurchaseOrderController::class, 'send'])->name('purchase-orders.send');
        Route::post('/purchase-orders/{purchase_order}/receive', [PurchaseOrderController::class, 'receive'])->name('purchase-orders.receive');
    });

    // CafePOS Management Routes
    Route::middleware(['auth', 'permission:inventory.manage'])->group(function () {
        Route::resource('suppliers', SupplierController::class)->except(['create', 'show', 'edit']);
        Route::resource('outlets', OutletController::class)->except(['create', 'show', 'edit']);
        Route::resource('ingredients', IngredientController::class)->except(['create', 'show', 'edit']);
        Route::post('/ingredients/adjust', [IngredientController::class, 'adjustStock'])->name('ingredients.adjust');
        Route::delete('/ingredients/transactions/{stockTransaction}', [IngredientController::class, 'destroyTransaction'])->name('ingredients.transactions.destroy');
        
        Route::resource('products', ProductController::class)->except(['create', 'show', 'edit']);
        Route::post('/products/{product}/recipe', [ProductController::class, 'saveRecipe'])->name('products.recipe.save');
        Route::post('/products/adjust', [ProductController::class, 'adjustProductStock'])->name('products.adjust');

        Route::resource('modifiers', ModifierController::class)->except(['create', 'show', 'edit']);

        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/print', [ReportController::class, 'print'])->name('reports.print');

        // Stock Opname & Waste
        Route::resource('opnames', OpnameController::class)->except(['create', 'edit']);
        Route::post('/opnames/{opname}/complete', [OpnameController::class, 'complete'])->name('opnames.complete');
        Route::resource('wastes', WasteController::class)->except(['create', 'show', 'edit']);
    });

        require __DIR__.'/auth.php';
    });
});

// Guest QR Self-Ordering Routes
Route::middleware(['license.verify'])->group(function () {
    Route::get('/order/outlet/{outlet}/table/{table}', [PosController::class, 'qrMenu'])->name('pos.qr-menu');
    Route::post('/order/outlet/{outlet}/table/{table}/checkout', [PosController::class, 'qrCheckout'])->name('pos.qr-checkout');
});

