<?php

use App\Http\Controllers\Api\V1\Auth\AuthController;
use App\Http\Controllers\Api\V1\BranchController;
use App\Http\Controllers\Api\V1\UserController;
use App\Http\Controllers\Api\V1\RoleManagement\RoleController;
use App\Http\Controllers\Api\V1\RoleManagement\PermissionController;
use App\Http\Controllers\Api\V1\Product\CategoryController;
use App\Http\Controllers\Api\V1\Product\ProductController;
use App\Http\Controllers\Api\V1\Product\OptionSetController;
use App\Http\Controllers\Api\V1\Product\InnerOptionSetController;
use App\Http\Controllers\Api\V1\Product\CupSizeController;
use App\Http\Controllers\Api\V1\Recipe\IngredientController;
use App\Http\Controllers\Api\V1\Recipe\RecipeController;
use App\Http\Controllers\Api\V1\Inventory\InventoryController;
use App\Http\Controllers\Api\V1\Inventory\PurchaseOrderController;
use App\Http\Controllers\Api\V1\Order\OrderController;
use App\Http\Controllers\Api\V1\Order\PaymentController;
use App\Http\Controllers\Api\V1\TempBillController;
use App\Http\Controllers\Api\V1\Production\ProductionBatchController;
use App\Http\Controllers\Api\V1\Expense\ExpenseController;
use App\Http\Controllers\Api\V1\Expense\CashRegisterController;
use App\Http\Controllers\Api\V1\Analytics\AnalyticsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Creamy Utopia API v1 Routes
|
*/

Route::prefix('v1')->group(function () {

    // Public Auth Routes
    Route::prefix('auth')->group(function () {
        Route::post('register', [AuthController::class, 'register']);
        Route::post('login', [AuthController::class, 'login']);
    });

    // Protected Routes
    Route::middleware(['auth:sanctum', 'api.logger'])->group(function () {

        // Auth Routes
        Route::prefix('auth')->group(function () {
            Route::post('logout', [AuthController::class, 'logout']);
            Route::get('user', [AuthController::class, 'user']);
            Route::post('refresh-token', [AuthController::class, 'refreshToken']);
        });

        // Branch Management
        Route::prefix('branches')->group(function () {
            Route::get('/', [BranchController::class, 'index'])->middleware('permission:branches.view');
            Route::post('/', [BranchController::class, 'store'])->middleware('permission:branches.create');
            Route::get('/{id}', [BranchController::class, 'show'])->middleware('permission:branches.view');
            Route::put('/{id}', [BranchController::class, 'update'])->middleware('permission:branches.update');
            Route::delete('/{id}', [BranchController::class, 'destroy'])->middleware('permission:branches.delete');
            Route::put('/{id}/settings', [BranchController::class, 'updateSettings'])->middleware('permission:branches.update');
        });

        // Role Management
        Route::prefix('roles')->group(function () {
            Route::get('/', [RoleController::class, 'index'])->middleware('permission:roles.view');
            Route::post('/', [RoleController::class, 'store'])->middleware('permission:roles.create');
            Route::get('/{id}', [RoleController::class, 'show'])->middleware('permission:roles.view');
            Route::put('/{id}', [RoleController::class, 'update'])->middleware('permission:roles.update');
            Route::delete('/{id}', [RoleController::class, 'destroy'])->middleware('permission:roles.delete');
            Route::post('/{id}/permissions', [RoleController::class, 'syncPermissions'])->middleware('permission:roles.assign');
            Route::get('/{id}/permissions', [RoleController::class, 'permissions'])->middleware('permission:roles.view');
        });

        // Permission Management
        Route::prefix('permissions')->group(function () {
            Route::get('/', [PermissionController::class, 'index'])->middleware('permission:permissions.view');
            Route::get('/by-module', [PermissionController::class, 'byModule'])->middleware('permission:permissions.view');
        });

        // User Management
        Route::prefix('users')->group(function () {
            Route::get('/', [UserController::class, 'index'])->middleware('permission:users.view');
            Route::post('/', [UserController::class, 'store'])->middleware('permission:users.create');
            Route::get('/{id}', [UserController::class, 'show'])->middleware('permission:users.view');
            Route::put('/{id}', [UserController::class, 'update'])->middleware('permission:users.update');
            Route::delete('/{id}', [UserController::class, 'destroy'])->middleware('permission:users.delete');
            Route::post('/{id}/assign-role', [UserController::class, 'assignRole'])->middleware('permission:roles.assign');
            Route::post('/{id}/revoke-role', [UserController::class, 'revokeRole'])->middleware('permission:roles.assign');
            Route::get('/{id}/permissions', [UserController::class, 'permissions'])->middleware('permission:users.view');
        });

        // =============================================
        // PRODUCT MANAGEMENT
        // =============================================

        // Categories
        Route::prefix('categories')->group(function () {
            Route::get('/', [CategoryController::class, 'index'])->middleware('permission:categories.view');
            Route::get('/active', [CategoryController::class, 'active'])->middleware('permission:categories.view');
            Route::get('/with-products', [CategoryController::class, 'withProducts'])->middleware('permission:categories.view');
            Route::post('/', [CategoryController::class, 'store'])->middleware('permission:categories.create');
            Route::get('/{id}', [CategoryController::class, 'show'])->middleware('permission:categories.view');
            Route::put('/{id}', [CategoryController::class, 'update'])->middleware('permission:categories.update');
            Route::delete('/{id}', [CategoryController::class, 'destroy'])->middleware('permission:categories.delete');
            Route::post('/{id}/restore', [CategoryController::class, 'restore'])->middleware('permission:categories.update');
        });

        // Products
        Route::prefix('products')->group(function () {
            Route::get('/', [ProductController::class, 'index'])->middleware('permission:products.view');
            Route::get('/active', [ProductController::class, 'active'])->middleware('permission:products.view');
            Route::get('/category/{categoryId}', [ProductController::class, 'byCategory'])->middleware('permission:products.view');
            Route::post('/', [ProductController::class, 'store'])->middleware('permission:products.create');
            Route::get('/{id}', [ProductController::class, 'show'])->middleware('permission:products.view');
            Route::put('/{id}', [ProductController::class, 'update'])->middleware('permission:products.update');
            Route::delete('/{id}', [ProductController::class, 'destroy'])->middleware('permission:products.delete');
            Route::post('/{id}/image', [ProductController::class, 'uploadImage'])->middleware('permission:products.update');
            Route::delete('/{id}/image', [ProductController::class, 'deleteImage'])->middleware('permission:products.update');
        });

        // Branch Products
        Route::prefix('branches/{branchId}/products')->group(function () {
            Route::get('/', [ProductController::class, 'forBranch'])->middleware('permission:products.view');
            Route::post('/{productId}', [ProductController::class, 'assignToBranch'])->middleware('permission:products.update');
            Route::put('/{productId}', [ProductController::class, 'updateBranchProduct'])->middleware('permission:products.update');
            Route::post('/bulk-assign', [ProductController::class, 'bulkAssignToBranch'])->middleware('permission:products.update');
        });

        // Option Sets
        Route::prefix('option-sets')->group(function () {
            Route::get('/', [OptionSetController::class, 'index'])->middleware('permission:option-sets.view');
            Route::get('/active', [OptionSetController::class, 'active'])->middleware('permission:option-sets.view');
            Route::get('/type/{type}', [OptionSetController::class, 'byType'])->middleware('permission:option-sets.view');
            Route::post('/', [OptionSetController::class, 'store'])->middleware('permission:option-sets.create');
            Route::get('/{id}', [OptionSetController::class, 'show'])->middleware('permission:option-sets.view');
            Route::put('/{id}', [OptionSetController::class, 'update'])->middleware('permission:option-sets.update');
            Route::delete('/{id}', [OptionSetController::class, 'destroy'])->middleware('permission:option-sets.delete');
            // Option Set Items
            Route::get('/{optionSetId}/items', [OptionSetController::class, 'items'])->middleware('permission:option-sets.view');
            Route::post('/{optionSetId}/items', [OptionSetController::class, 'storeItem'])->middleware('permission:option-sets.create');
            Route::put('/{optionSetId}/items/{itemId}', [OptionSetController::class, 'updateItem'])->middleware('permission:option-sets.update');
            Route::delete('/{optionSetId}/items/{itemId}', [OptionSetController::class, 'destroyItem'])->middleware('permission:option-sets.delete');
        });

        // Inner Option Sets (Toppings/Bases)
        Route::prefix('inner-option-sets')->group(function () {
            Route::get('/', [InnerOptionSetController::class, 'index'])->middleware('permission:inner-option-sets.view');
            Route::get('/active', [InnerOptionSetController::class, 'active'])->middleware('permission:inner-option-sets.view');
            Route::get('/type/{type}', [InnerOptionSetController::class, 'byType'])->middleware('permission:inner-option-sets.view');
            Route::post('/', [InnerOptionSetController::class, 'store'])->middleware('permission:inner-option-sets.create');
            Route::get('/{id}', [InnerOptionSetController::class, 'show'])->middleware('permission:inner-option-sets.view');
            Route::put('/{id}', [InnerOptionSetController::class, 'update'])->middleware('permission:inner-option-sets.update');
            Route::delete('/{id}', [InnerOptionSetController::class, 'destroy'])->middleware('permission:inner-option-sets.delete');
            // Inner Option Items
            Route::get('/{innerOptionSetId}/items', [InnerOptionSetController::class, 'items'])->middleware('permission:inner-option-sets.view');
            Route::post('/{innerOptionSetId}/items', [InnerOptionSetController::class, 'storeItem'])->middleware('permission:inner-option-sets.create');
            Route::put('/{innerOptionSetId}/items/{itemId}', [InnerOptionSetController::class, 'updateItem'])->middleware('permission:inner-option-sets.update');
            Route::delete('/{innerOptionSetId}/items/{itemId}', [InnerOptionSetController::class, 'destroyItem'])->middleware('permission:inner-option-sets.delete');
        });

        // Cup Sizes (Weight-based tracking)
        Route::prefix('cup-sizes')->group(function () {
            Route::get('/', [CupSizeController::class, 'index']); // Public endpoint - no permission required
            Route::get('/{id}', [CupSizeController::class, 'show']); // Public endpoint
            Route::post('/', [CupSizeController::class, 'store'])->middleware('permission:products.create');
            Route::put('/{id}', [CupSizeController::class, 'update'])->middleware('permission:products.update');
            Route::delete('/{id}', [CupSizeController::class, 'destroy'])->middleware('permission:products.delete');
        });

        // =============================================
        // RECIPE MANAGEMENT
        // =============================================

        // Ingredients
        Route::prefix('ingredients')->group(function () {
            Route::get('/', [IngredientController::class, 'index'])->middleware('permission:ingredients.view');
            Route::get('/active', [IngredientController::class, 'active'])->middleware('permission:ingredients.view');
            Route::get('/search', [IngredientController::class, 'search'])->middleware('permission:ingredients.view');
            Route::get('/low-stock', [IngredientController::class, 'lowStock'])->middleware('permission:ingredients.view');
            Route::post('/', [IngredientController::class, 'store'])->middleware('permission:ingredients.create');
            Route::get('/{id}', [IngredientController::class, 'show'])->middleware('permission:ingredients.view');
            Route::put('/{id}', [IngredientController::class, 'update'])->middleware('permission:ingredients.update');
            Route::delete('/{id}', [IngredientController::class, 'destroy'])->middleware('permission:ingredients.delete');
        });

        // Recipes
        Route::prefix('recipes')->group(function () {
            Route::get('/', [RecipeController::class, 'index'])->middleware('permission:recipes.view');
            Route::get('/active', [RecipeController::class, 'active'])->middleware('permission:recipes.view');
            Route::get('/product/{productId}', [RecipeController::class, 'forProduct'])->middleware('permission:recipes.view');
            Route::post('/', [RecipeController::class, 'store'])->middleware('permission:recipes.create');
            Route::get('/{id}', [RecipeController::class, 'show'])->middleware('permission:recipes.view');
            Route::put('/{id}', [RecipeController::class, 'update'])->middleware('permission:recipes.update');
            Route::delete('/{id}', [RecipeController::class, 'destroy'])->middleware('permission:recipes.delete');
            Route::get('/{id}/cost', [RecipeController::class, 'calculateCost'])->middleware('permission:recipes.view');
        });

        // Recipe Production Items (gram-based batch deductions)
        Route::prefix('recipes/{recipeId}/production-items')->group(function () {
            Route::get('/', [RecipeController::class, 'productionItems'])->middleware('permission:recipes.view');
            Route::post('/', [RecipeController::class, 'storeProductionItem'])->middleware('permission:recipes.create');
            Route::post('/sync', [RecipeController::class, 'syncProductionItems'])->middleware('permission:recipes.update');
            Route::put('/{itemId}', [RecipeController::class, 'updateProductionItem'])->middleware('permission:recipes.update');
            Route::delete('/{itemId}', [RecipeController::class, 'destroyProductionItem'])->middleware('permission:recipes.delete');
        });

        // Scoop Yields
        Route::prefix('scoop-yields')->group(function () {
            Route::get('/', [RecipeController::class, 'scoopYields'])->middleware('permission:recipes.view');
            Route::get('/ingredient/{ingredientId}', [RecipeController::class, 'scoopYieldsForIngredient'])->middleware('permission:recipes.view');
            Route::post('/', [RecipeController::class, 'storeScoopYield'])->middleware('permission:recipes.create');
            Route::put('/{id}', [RecipeController::class, 'updateScoopYield'])->middleware('permission:recipes.update');
            Route::delete('/{id}', [RecipeController::class, 'destroyScoopYield'])->middleware('permission:recipes.delete');
            Route::post('/calculate-usage', [RecipeController::class, 'calculateScoopUsage'])->middleware('permission:recipes.view');
        });

        // =============================================
        // INVENTORY MANAGEMENT
        // =============================================

        // Branch Inventory
        Route::prefix('branches/{branchId}/inventory')->group(function () {
            Route::get('/stock', [InventoryController::class, 'stock'])->middleware('permission:stock.view');
            Route::get('/stock/{ingredientId}', [InventoryController::class, 'stockForIngredient'])->middleware('permission:stock.view');
            Route::get('/low-stock', [InventoryController::class, 'lowStock'])->middleware('permission:stock.view');
            Route::get('/out-of-stock', [InventoryController::class, 'outOfStock'])->middleware('permission:stock.view');
            Route::post('/add-stock', [InventoryController::class, 'addStock'])->middleware('permission:stock.create');
            Route::post('/deduct-stock', [InventoryController::class, 'deductStock'])->middleware('permission:stock.update');
            Route::post('/adjust-stock', [InventoryController::class, 'adjustStock'])->middleware('permission:stock.adjust');
            Route::post('/record-waste', [InventoryController::class, 'recordWaste'])->middleware('permission:stock.update');
            Route::get('/transactions', [InventoryController::class, 'transactions'])->middleware('permission:stock.view');
            Route::get('/transactions/ingredient/{ingredientId}', [InventoryController::class, 'transactionsForIngredient'])->middleware('permission:stock.view');
            Route::get('/transactions/date-range', [InventoryController::class, 'transactionsByDateRange'])->middleware('permission:stock.view');
            Route::get('/alerts', [InventoryController::class, 'alerts'])->middleware('permission:stock.view');
            Route::post('/alerts/{alertId}/resolve', [InventoryController::class, 'resolveAlert'])->middleware('permission:stock.update');
        });

        // Purchase Orders
        Route::prefix('purchase-orders')->group(function () {
            Route::get('/', [PurchaseOrderController::class, 'index'])->middleware('permission:stock.view');
            Route::get('/branch/{branchId}', [PurchaseOrderController::class, 'forBranch'])->middleware('permission:stock.view');
            Route::get('/branch/{branchId}/status/{status}', [PurchaseOrderController::class, 'byStatus'])->middleware('permission:stock.view');
            Route::get('/branch/{branchId}/pending', [PurchaseOrderController::class, 'pending'])->middleware('permission:stock.view');
            Route::post('/', [PurchaseOrderController::class, 'store'])->middleware('permission:stock.create');
            Route::get('/{id}', [PurchaseOrderController::class, 'show'])->middleware('permission:stock.view');
            Route::put('/{id}', [PurchaseOrderController::class, 'update'])->middleware('permission:stock.update');
            Route::delete('/{id}', [PurchaseOrderController::class, 'destroy'])->middleware('permission:stock.update');
            Route::put('/{id}/status', [PurchaseOrderController::class, 'updateStatus'])->middleware('permission:stock.update');
            Route::post('/{id}/receive', [PurchaseOrderController::class, 'receive'])->middleware('permission:stock.create');
            Route::post('/{id}/cancel', [PurchaseOrderController::class, 'cancel'])->middleware('permission:stock.update');
        });

        // =============================================
        // PRODUCTION BATCH MANAGEMENT
        // =============================================

        // Batch Size Configurations (Bowl Settings - Admin only for CUD)
        Route::prefix('batch-size-configs')->group(function () {
            Route::get('/', [ProductionBatchController::class, 'batchSizeConfigs'])->middleware('permission:batch-size-configs.view');
            Route::post('/', [ProductionBatchController::class, 'storeBatchSizeConfig'])->middleware('permission:batch-size-configs.create');
            Route::put('/{id}', [ProductionBatchController::class, 'updateBatchSizeConfig'])->middleware('permission:batch-size-configs.update');
            Route::delete('/{id}', [ProductionBatchController::class, 'destroyBatchSizeConfig'])->middleware('permission:batch-size-configs.delete');
        });

        // Production Batches
        Route::prefix('production-batches')->group(function () {
            Route::get('/', [ProductionBatchController::class, 'index'])->middleware('permission:stock.view');
            Route::get('/active', [ProductionBatchController::class, 'active'])->middleware('permission:stock.view');
            Route::post('/', [ProductionBatchController::class, 'store'])->middleware('permission:stock.create');
            Route::get('/{id}', [ProductionBatchController::class, 'show'])->middleware('permission:stock.view');
            Route::put('/{id}', [ProductionBatchController::class, 'update'])->middleware('permission:stock.update');
            Route::post('/{id}/cancel', [ProductionBatchController::class, 'cancel'])->middleware('permission:stock.update');
            Route::post('/{id}/waste', [ProductionBatchController::class, 'recordWaste'])->middleware('permission:stock.update');
            Route::post('/mark-expired', [ProductionBatchController::class, 'markExpired'])->middleware('permission:stock.update');
            // Reports
            Route::get('/reports/daily-summary', [ProductionBatchController::class, 'dailySummary'])->middleware('permission:analytics.view-sales');
            Route::get('/reports/daily-sales', [ProductionBatchController::class, 'dailySales'])->middleware('permission:analytics.view-sales');
            Route::get('/reports/sales', [ProductionBatchController::class, 'salesReport'])->middleware('permission:analytics.view-sales');
        });

        // =============================================
        // ORDER MANAGEMENT & POS
        // =============================================

        // Temp Bills Management
        Route::prefix('temp-bills')->group(function () {
            Route::get('/', [TempBillController::class, 'index'])->middleware('permission:orders.view');
            Route::post('/', [TempBillController::class, 'store'])->middleware('permission:orders.create');
            Route::get('/{id}', [TempBillController::class, 'show'])->middleware('permission:orders.view');
            Route::post('/{id}/items', [TempBillController::class, 'addItem'])->middleware('permission:orders.update');
            Route::delete('/{id}/items/{itemId}', [TempBillController::class, 'removeItem'])->middleware('permission:orders.update');
            Route::put('/{id}/items/{itemId}', [TempBillController::class, 'updateItem'])->middleware('permission:orders.update');
            Route::post('/{id}/lock', [TempBillController::class, 'lockForPayment'])->middleware('permission:orders.update');
            Route::post('/{id}/cancel', [TempBillController::class, 'cancel'])->middleware('permission:orders.cancel');
            Route::get('/{id}/modifications', [TempBillController::class, 'getModifications'])->middleware('permission:orders.view');
        });

        // Payment Processing
        Route::prefix('payments')->group(function () {
            Route::post('/orders/{id}/process', [PaymentController::class, 'processPayment'])->middleware('permission:orders.create');
            Route::get('/cash-register/balance', [PaymentController::class, 'getCashRegisterBalance'])->middleware('permission:orders.view');
            Route::get('/orders/{id}/print-bill', [PaymentController::class, 'printFinalBill'])->middleware('permission:orders.view');
        });

        // Orders
        Route::prefix('orders')->group(function () {
            Route::get('/', [OrderController::class, 'index'])->middleware('permission:orders.view');
            Route::get('/active', [OrderController::class, 'activeAll'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}', [OrderController::class, 'forBranch'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}/active', [OrderController::class, 'activeForBranch'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}/status/{status}', [OrderController::class, 'byStatus'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}/today', [OrderController::class, 'todayForBranch'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}/date-range', [OrderController::class, 'byDateRange'])->middleware('permission:orders.view');
            Route::get('/branch/{branchId}/daily-summary', [OrderController::class, 'dailySummary'])->middleware('permission:orders.view');
            // Temp Bills
            Route::get('/branch/{branchId}/temp-bills', [OrderController::class, 'getTempBills'])->middleware('permission:orders.view');
            Route::post('/temp-bill', [OrderController::class, 'createTempBill'])->middleware('permission:orders.create');
            Route::put('/{id}/temp-bill', [OrderController::class, 'updateTempBill'])->middleware('permission:orders.update');
            Route::post('/{id}/finalize-temp-bill', [OrderController::class, 'finalizeTempBill'])->middleware('permission:orders.create');
            Route::get('/{id}/duplicate-bill', [OrderController::class, 'getDuplicateBill'])->middleware('permission:orders.view');
            // Regular Orders
            Route::post('/', [OrderController::class, 'store'])->middleware('permission:orders.create');
            Route::get('/{id}', [OrderController::class, 'show'])->middleware('permission:orders.view');
            Route::put('/{id}', [OrderController::class, 'update'])->middleware('permission:orders.update');
            Route::delete('/{id}', [OrderController::class, 'destroy'])->middleware('permission:orders.delete');
            Route::put('/{id}/status', [OrderController::class, 'updateStatus'])->middleware('permission:orders.update');
            Route::post('/{id}/cancel', [OrderController::class, 'cancel'])->middleware('permission:orders.cancel');
            Route::post('/{id}/discount', [OrderController::class, 'applyDiscount'])->middleware('permission:pos.apply-discount');
            // Payments
            Route::get('/{orderId}/payments', [OrderController::class, 'payments'])->middleware('permission:orders.view');
            Route::post('/{orderId}/payments', [OrderController::class, 'addPayment'])->middleware('permission:orders.update');
            Route::post('/{orderId}/payments/{paymentId}/refund', [OrderController::class, 'refundPayment'])->middleware('permission:orders.update');
        });

        // =============================================
        // EXPENSE MANAGEMENT
        // =============================================

        // Expense Categories (viewing uses expenses.view permission for easier access)
        Route::prefix('expense-categories')->group(function () {
            Route::get('/', [ExpenseController::class, 'categoryIndex'])->middleware('permission:expenses.view');
            Route::post('/', [ExpenseController::class, 'categoryStore'])->middleware('permission:expense-categories.create');
            Route::get('/{id}', [ExpenseController::class, 'categoryShow'])->middleware('permission:expenses.view');
            Route::put('/{id}', [ExpenseController::class, 'categoryUpdate'])->middleware('permission:expense-categories.update');
            Route::delete('/{id}', [ExpenseController::class, 'categoryDestroy'])->middleware('permission:expense-categories.delete');
        });

        // Expenses
        Route::prefix('expenses')->group(function () {
            Route::get('/', [ExpenseController::class, 'index'])->middleware('permission:expenses.view');
            Route::get('/branch/{branchId}', [ExpenseController::class, 'byBranch'])->middleware('permission:expenses.view');
            Route::post('/', [ExpenseController::class, 'store'])->middleware('permission:expenses.create');
            Route::get('/{id}', [ExpenseController::class, 'show'])->middleware('permission:expenses.view');
            Route::put('/{id}', [ExpenseController::class, 'update'])->middleware('permission:expenses.update');
            Route::delete('/{id}', [ExpenseController::class, 'destroy'])->middleware('permission:expenses.delete');
            Route::post('/{id}/approve', [ExpenseController::class, 'approve'])->middleware('permission:expenses.update');
            Route::post('/{id}/reject', [ExpenseController::class, 'reject'])->middleware('permission:expenses.update');
        });

        // Cash Registers
        Route::prefix('cash-registers')->group(function () {
            Route::get('/', [CashRegisterController::class, 'index'])->middleware('permission:cash-flows.view');
            Route::get('/{id}', [CashRegisterController::class, 'show'])->middleware('permission:cash-flows.view');
            Route::get('/{id}/transactions', [CashRegisterController::class, 'transactions'])->middleware('permission:cash-flows.view');
            Route::get('/{id}/summary', [CashRegisterController::class, 'summary'])->middleware('permission:cash-flows.view');
            Route::post('/{id}/close', [CashRegisterController::class, 'close'])->middleware('permission:cash-flows.create');
            Route::post('/{id}/add-cash', [CashRegisterController::class, 'addCash'])->middleware('permission:cash-flows.create');
            Route::post('/{id}/remove-cash', [CashRegisterController::class, 'removeCash'])->middleware('permission:cash-flows.create');
        });

        // Branch Cash Registers
        Route::prefix('branches/{branchId}/cash-register')->group(function () {
            Route::post('/open', [CashRegisterController::class, 'open'])->middleware('permission:cash-flows.create');
            Route::get('/current', [CashRegisterController::class, 'currentByBranch'])->middleware('permission:cash-flows.view');
        });

        // =============================================
        // ANALYTICS & REPORTS
        // =============================================

        Route::prefix('analytics')->group(function () {
            Route::get('/dashboard', [AnalyticsController::class, 'dashboard'])->middleware('permission:analytics.view-dashboard');
            Route::get('/sales', [AnalyticsController::class, 'salesReport'])->middleware('permission:analytics.view-sales');
            Route::get('/expenses', [AnalyticsController::class, 'expenseReport'])->middleware('permission:analytics.view-expenses');
            Route::get('/profit-loss', [AnalyticsController::class, 'profitLoss'])->middleware('permission:analytics.view-profitability');
            Route::get('/top-products', [AnalyticsController::class, 'topProducts'])->middleware('permission:analytics.view-sales');
            Route::get('/hourly-sales', [AnalyticsController::class, 'hourlySales'])->middleware('permission:analytics.view-sales');
            Route::get('/sales-by-category', [AnalyticsController::class, 'salesByCategory'])->middleware('permission:analytics.view-sales');
            Route::get('/sales-by-payment-method', [AnalyticsController::class, 'salesByPaymentMethod'])->middleware('permission:analytics.view-sales');
            Route::get('/branch-comparison', [AnalyticsController::class, 'branchComparison'])->middleware('permission:analytics.view-profitability');
            Route::get('/inventory-valuation', [AnalyticsController::class, 'inventoryValuation'])->middleware('permission:stock.view');
            Route::get('/low-stock-alerts', [AnalyticsController::class, 'lowStockAlerts'])->middleware('permission:stock.view');
        });

    });

});
