<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BonusController;
use App\Http\Controllers\CustomerPortalController;
use App\Http\Controllers\CustomerRequestController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DigitalFormController;
use App\Http\Controllers\InventoryController;
use App\Http\Controllers\LandingPageController;
use App\Http\Controllers\LeaderboardController;
use App\Http\Controllers\LaundryOrderController;
use App\Http\Controllers\MasterDataController;
use App\Http\Controllers\OperationalExpenseController;
use App\Http\Controllers\PermissionMatrixController;
use App\Http\Controllers\PosController;
use App\Http\Controllers\PortalPreviewController;
use App\Http\Controllers\PublicTrackingController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\StockTransferController;
use Illuminate\Support\Facades\Route;

Route::get('/', LandingPageController::class)->name('landing');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'create'])->name('login');
    Route::post('/login', [AuthController::class, 'store'])->name('login.store')->middleware('throttle:6,1');
});

Route::get('/track', [PublicTrackingController::class, 'index'])->name('tracking.index');
Route::post('/track', [PublicTrackingController::class, 'show'])->name('tracking.show')->middleware('throttle:20,1');

Route::prefix('portal')->name('portal.')->group(function () {
    Route::get('/login', [CustomerPortalController::class, 'login'])->name('login');
    Route::post('/login', [CustomerPortalController::class, 'authenticate'])->name('authenticate')->middleware('throttle:8,1');
    Route::middleware('portal.customer')->group(function () {
        Route::get('/', [CustomerPortalController::class, 'dashboard'])->name('dashboard');
        Route::get('/orders/{order}', [CustomerPortalController::class, 'order'])->name('orders.show');
        Route::get('/requests', [CustomerPortalController::class, 'requests'])->name('requests');
        Route::get('/account', [CustomerPortalController::class, 'account'])->name('account');
        Route::put('/account', [CustomerPortalController::class, 'updateAccount'])->name('account.update');
        Route::get('/notifications', [CustomerPortalController::class, 'notifications'])->name('notifications');
        Route::post('/notifications/{notification}/read', [CustomerPortalController::class, 'markNotificationRead'])->name('notifications.read');
        Route::get('/notifications/summary', [CustomerPortalController::class, 'notificationSummary'])->name('notifications.summary');
        Route::post('/push-subscriptions', [CustomerPortalController::class, 'subscribePush'])->name('push-subscriptions.store');
        Route::post('/pin', [CustomerPortalController::class, 'updatePin'])->name('pin.update');
        Route::post('/requests', [CustomerPortalController::class, 'request'])->name('requests.store');
        Route::post('/logout', [CustomerPortalController::class, 'logout'])->name('logout');
    });
});

Route::middleware(['auth', 'branch'])->group(function () {
    Route::post('/logout', [AuthController::class, 'destroy'])->name('logout');
    Route::post('/password', [AuthController::class, 'password'])->name('password.update');
    Route::get('/dashboard', DashboardController::class)->name('dashboard')->middleware('permission:dashboard');

    Route::get('/pos', [PosController::class, 'create'])->name('pos.create')->middleware('permission:pos');
    Route::post('/pos', [PosController::class, 'store'])->name('pos.store')->middleware('permission:pos');
    Route::get('/pos/customers/search', [PosController::class, 'searchCustomers'])->name('pos.customers.search')->middleware('permission:pos,mobile_intake');
    Route::get('/pos/products/search', [PosController::class, 'searchProducts'])->name('pos.products.search')->middleware('permission:pos,mobile_intake');
    Route::post('/pos/customers', [PosController::class, 'quickCustomer'])->name('pos.customers.store')->middleware('permission:pos');
    Route::get('/mobile-intake', [PosController::class, 'mobileIntake'])->name('mobile-intake.create')->middleware('permission:mobile_intake');
    Route::post('/mobile-intake', [PosController::class, 'mobileStore'])->name('mobile-intake.store')->middleware('permission:mobile_intake');

    Route::get('/orders', [LaundryOrderController::class, 'index'])->name('orders.index')->middleware('permission:orders');
    Route::get('/orders/{order}', [LaundryOrderController::class, 'show'])->name('orders.show')->middleware('permission:orders');
    Route::get('/orders/{order}/receipt', [LaundryOrderController::class, 'receipt'])->name('orders.receipt')->middleware('permission:orders');
    Route::post('/orders/{order}/status', [LaundryOrderController::class, 'updateStatus'])->name('orders.status')->middleware('permission:orders_update');
    Route::post('/orders/{order}/progress', [LaundryOrderController::class, 'updateProgress'])->name('orders.progress')->middleware('permission:orders_update');
    Route::post('/orders/{order}/payments', [LaundryOrderController::class, 'storePayment'])->name('orders.payments.store')->middleware('permission:payments');
    Route::post('/orders/{order}/details/{orderDetail}/progress', [LaundryOrderController::class, 'updateDetailProgress'])->name('orders.details.progress')->middleware('permission:orders_update');
    Route::post('/orders/{order}/whatsapp', [LaundryOrderController::class, 'sendWhatsApp'])->name('orders.whatsapp')->middleware('permission:orders_update');

    Route::get('/orders/{order}/digital-form', [DigitalFormController::class, 'create'])->name('digital-forms.create')->middleware('permission:digital_forms');
    Route::post('/orders/{order}/digital-form', [DigitalFormController::class, 'store'])->name('digital-forms.store')->middleware('permission:digital_forms');
    Route::get('/digital-forms/{digitalForm}', [DigitalFormController::class, 'show'])->name('digital-forms.show')->middleware('permission:digital_forms');

    Route::get('/master/{module}', [MasterDataController::class, 'index'])->name('master.index')->middleware('permission:master_data');
    Route::post('/master/{module}', [MasterDataController::class, 'store'])->name('master.store')->middleware(['role:owner,admin_cabang,manager', 'permission:master_data']);
    Route::put('/master/{module}/{id}', [MasterDataController::class, 'update'])->name('master.update')->middleware(['role:owner,admin_cabang,manager', 'permission:master_data']);
    Route::delete('/master/{module}/{id}', [MasterDataController::class, 'destroy'])->name('master.destroy')->middleware(['role:owner,admin_cabang,manager', 'permission:master_data']);
    Route::post('/master/customers/{customer}/reset-pin', [MasterDataController::class, 'resetCustomerPin'])->name('master.customers.reset-pin')->middleware(['role:owner,admin_cabang,manager', 'permission:master_data']);

    Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index')->middleware('permission:inventory');
    Route::post('/inventory/purchases', [InventoryController::class, 'purchase'])->name('inventory.purchases.store')->middleware('permission:inventory');
    Route::post('/inventory/opname', [InventoryController::class, 'opname'])->name('inventory.opname.store')->middleware('permission:inventory');
    Route::post('/inventory/adjustments', [InventoryController::class, 'adjust'])->name('inventory.adjustments.store')->middleware('permission:inventory');

    Route::get('/stock-transfers', [StockTransferController::class, 'index'])->name('stock-transfers.index')->middleware('permission:stock_transfers');
    Route::post('/stock-transfers', [StockTransferController::class, 'store'])->name('stock-transfers.store')->middleware('permission:stock_transfers');
    Route::post('/stock-transfers/{stockTransfer}/approve', [StockTransferController::class, 'approve'])->name('stock-transfers.approve')->middleware('permission:stock_transfer_approval');
    Route::post('/stock-transfers/{stockTransfer}/reject', [StockTransferController::class, 'reject'])->name('stock-transfers.reject')->middleware('permission:stock_transfer_approval');
    Route::post('/stock-transfers/{stockTransfer}/cancel', [StockTransferController::class, 'cancel'])->name('stock-transfers.cancel')->middleware('permission:stock_transfers');

    Route::get('/customer-requests', [CustomerRequestController::class, 'index'])->name('customer-requests.index')->middleware('permission:customer_requests');
    Route::get('/customer-requests/{customerOrderRequest}/portal-preview', [CustomerRequestController::class, 'previewPortal'])->name('customer-requests.portal-preview')->middleware('permission:portal_preview');
    Route::post('/customer-requests/{customerOrderRequest}/review', [CustomerRequestController::class, 'review'])->name('customer-requests.review')->middleware('permission:customer_requests');
    Route::post('/customer-requests/{customerOrderRequest}/cancel', [CustomerRequestController::class, 'cancel'])->name('customer-requests.cancel')->middleware('permission:customer_requests');
    Route::post('/customer-requests/{customerOrderRequest}/convert', [CustomerRequestController::class, 'convert'])->name('customer-requests.convert')->middleware('permission:customer_requests');

    Route::get('/portal-preview', [PortalPreviewController::class, 'index'])->name('portal-preview.index')->middleware('permission:portal_preview');
    Route::get('/portal-preview/customers/{customer}', [PortalPreviewController::class, 'customer'])->name('portal-preview.customer')->middleware('permission:portal_preview');
    Route::get('/portal-preview/customers/{customer}/requests', [PortalPreviewController::class, 'requests'])->name('portal-preview.requests')->middleware('permission:portal_preview');
    Route::get('/portal-preview/customers/{customer}/account', [PortalPreviewController::class, 'account'])->name('portal-preview.account')->middleware('permission:portal_preview');
    Route::get('/portal-preview/customers/{customer}/orders/{order}', [PortalPreviewController::class, 'order'])->name('portal-preview.order')->middleware('permission:portal_preview');

    Route::get('/reports', [ReportController::class, 'index'])->name('reports.index')->middleware('permission:reports');
    Route::get('/expenses', [OperationalExpenseController::class, 'index'])->name('expenses.index')->middleware('permission:expenses');
    Route::post('/expenses', [OperationalExpenseController::class, 'store'])->name('expenses.store')->middleware('permission:expenses');
    Route::put('/expenses/{expense}', [OperationalExpenseController::class, 'update'])->name('expenses.update')->middleware('permission:expenses');
    Route::delete('/expenses/{expense}', [OperationalExpenseController::class, 'destroy'])->name('expenses.destroy')->middleware('permission:expenses');
    Route::post('/reports/bonus/disburse', [ReportController::class, 'disburseBonus'])->name('reports.bonus.disburse')->middleware(['role:owner', 'permission:bonus_disbursement']);
    Route::get('/leaderboards', [LeaderboardController::class, 'index'])->name('leaderboards.index')->middleware('permission:leaderboards');
    Route::get('/bonuses', [BonusController::class, 'index'])->name('bonuses.index')->middleware('permission:bonuses');
    Route::post('/bonuses/disburse', [BonusController::class, 'disburse'])->name('bonuses.disburse')->middleware(['role:owner,admin_cabang', 'permission:bonus_disbursement']);
    Route::get('/bonuses/settings', [BonusController::class, 'settings'])->name('bonuses.settings')->middleware(['role:owner,admin_cabang,manager', 'permission:bonuses']);
    Route::post('/bonuses/settings', [BonusController::class, 'updateSettings'])->name('bonuses.settings.update')->middleware(['role:owner,admin_cabang,manager', 'permission:bonuses']);
    Route::get('/bonuses/disbursements/{bonusDisbursement}', [BonusController::class, 'showDisbursement'])->name('bonuses.disbursements.show')->middleware('permission:bonuses');

    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index')->middleware(['role:owner,admin_cabang', 'permission:settings']);
    Route::post('/settings', [SettingsController::class, 'store'])->name('settings.store')->middleware(['role:owner,admin_cabang', 'permission:settings']);
    Route::get('/permissions', [PermissionMatrixController::class, 'index'])->name('permissions.index')->middleware('role:owner');
    Route::post('/permissions', [PermissionMatrixController::class, 'update'])->name('permissions.update')->middleware('role:owner');
});
