<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\MasterMejaController;
use App\Http\Controllers\MasterMenuController;
use App\Http\Controllers\MasterStandController;
use App\Http\Controllers\MasterUserController;
use App\Http\Controllers\MasterWaiterController;
use App\Http\Controllers\MasterBankAccountController;
use App\Http\Controllers\MasterDiscountController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\MasterSettingController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\AttendanceController;
use App\Http\Controllers\AttendanceUserRequestController;
use App\Http\Controllers\AttendanceUserController;
use App\Http\Controllers\CashierShiftController;
use App\Http\Controllers\WarehouseItemController;
use App\Http\Controllers\WarehouseInController;
use App\Http\Controllers\WarehouseOutController;
use App\Http\Controllers\WarehouseReportController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    if (auth()->check()) {
        $role = auth()->user()?->role?->nama_roles;

        if ($role === 'admin') {
            return redirect()->route('dashboard');
        }

        return in_array($role, ['cashier', 'supervisor'], true)
            ? ($role === 'cashier' ? redirect()->route('dashboard.cashier') : redirect()->route('orders.all'))
            : redirect()->route('orders.index');
    }

    return redirect()->route('login');
})->name('home');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.attempt');
});

Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'admin'])
        ->middleware('role:admin')
        ->name('dashboard');
    Route::get('/dashboard/cashier', [DashboardController::class, 'cashier'])
        ->middleware('role:cashier')
        ->name('dashboard.cashier');
    Route::prefix('master')->middleware('role:admin,supervisor')->group(function () {
        Route::resource('stands', MasterStandController::class)->except('show');
        Route::resource('mejas', MasterMejaController::class)->except('show');
        Route::resource('waiters', MasterWaiterController::class)->except('show');
        Route::resource('menus', MasterMenuController::class)->except('show');
    });

    Route::prefix('master')->middleware('role:admin')->group(function () {
        Route::resource('users', MasterUserController::class)->except('show');
        Route::resource('banks', MasterBankAccountController::class)->except('show');
        Route::get('settings', [MasterSettingController::class, 'edit'])->name('settings.edit');
        Route::put('settings', [MasterSettingController::class, 'update'])->name('settings.update');
    });

    Route::middleware('role:admin')->group(function () {
        Route::get('/reports/sales', [ReportController::class, 'sales'])->name('reports.sales.index');
        Route::get('/reports/sales/export', [ReportController::class, 'exportSales'])->name('reports.sales.export');
        Route::get('/reports/profit', [ReportController::class, 'profit'])->name('reports.profit.index');
        Route::get('/reports/profit/export', [ReportController::class, 'exportProfit'])->name('reports.profit.export');
        Route::get('/reports/menu-sales', [ReportController::class, 'menuSales'])->name('reports.menu_sales.index');
        Route::get('/reports/menu-sales/export', [ReportController::class, 'exportMenuSales'])->name('reports.menu_sales.export');
    });

    Route::get('/orders', [OrderController::class, 'index'])->middleware('role:waiter,supervisor,cashier')->name('orders.index');
    Route::get('/orders/create', [OrderController::class, 'create'])->middleware('role:waiter,supervisor,cashier')->name('orders.create');
    Route::post('/orders', [OrderController::class, 'store'])->middleware('role:waiter,supervisor,cashier')->name('orders.store');

    Route::get('/orders/all', [OrderController::class, 'all'])->middleware('role:admin,cashier,supervisor')->name('orders.all');
    Route::get('/orders/recap/print', [OrderController::class, 'recapPrint'])->middleware('role:cashier')->name('orders.recap.print');

    Route::resource('/discounts', MasterDiscountController::class)->middleware('role:admin')->names([
        'index' => 'discounts.index',
        'create' => 'discounts.create',
        'store' => 'discounts.store',
        'edit' => 'discounts.edit',
        'update' => 'discounts.update',
        'destroy' => 'discounts.destroy',
        'show' => 'discounts.show',
    ])->except('show');
    Route::get('/orders/{order}/close-bill', [OrderController::class, 'closeBill'])->middleware('role:cashier')->name('orders.close_bill');
    Route::post('/orders/{order}/close', [OrderController::class, 'close'])->middleware('role:cashier')->name('orders.close');
    Route::post('/orders/{order}/cancel', [OrderController::class, 'cancel'])->middleware('role:admin')->name('orders.cancel');

Route::get('/orders/{order}', [OrderController::class, 'show'])->name('orders.show');
Route::post('/orders/{order}/move-table', [OrderController::class, 'moveTable'])->name('orders.move_table');
    Route::get('/orders/{order}/print', [OrderController::class, 'printBill'])->name('orders.print');
    Route::post('/orders/{order}/items', [OrderController::class, 'addItems'])
        ->middleware('role:admin,cashier,waiter')
        ->name('orders.items.store');
    Route::post('/orders/{order}/items/{detail}/cancel', [OrderController::class, 'cancelItem'])
        ->middleware('role:supervisor,waiter,admin')
        ->name('orders.items.cancel');
    Route::post('/orders/{order}/items/{detail}/update-qty', [OrderController::class, 'updateItemQty'])
        ->middleware('role:supervisor,waiter')
        ->name('orders.items.update_qty');

    Route::post('/cashier-shifts/open', [CashierShiftController::class, 'open'])
        ->middleware('role:cashier')
        ->name('cashier_shifts.open');
    Route::post('/cashier-shifts/close', [CashierShiftController::class, 'close'])
        ->middleware('role:cashier')
        ->name('cashier_shifts.close');

    Route::get('/attendance', [AttendanceController::class, 'index'])
        ->middleware('attendance.access')
        ->name('attendance.index');
    Route::get('/attendance/users', [AttendanceUserController::class, 'index'])
        ->middleware('attendance.users.access')
        ->name('attendance.users.index');
    Route::post('/attendance/users', [AttendanceUserRequestController::class, 'store'])
        ->middleware('attendance.users.access')
        ->name('attendance.users.store');

    Route::prefix('warehouse')->name('warehouse.')->middleware('warehouse.access')->group(function () {
        Route::resource('items', WarehouseItemController::class)->except('show');
        Route::get('in', [WarehouseInController::class, 'index'])->name('in.index');
        Route::get('in/create', [WarehouseInController::class, 'create'])->name('in.create');
        Route::post('in', [WarehouseInController::class, 'store'])->name('in.store');
        Route::delete('in/{in}', [WarehouseInController::class, 'destroy'])->name('in.destroy');

        Route::get('out', [WarehouseOutController::class, 'index'])->name('out.index');
        Route::get('out/create', [WarehouseOutController::class, 'create'])->name('out.create');
        Route::post('out', [WarehouseOutController::class, 'store'])->name('out.store');
        Route::delete('out/{out}', [WarehouseOutController::class, 'destroy'])->name('out.destroy');

        Route::get('report', [WarehouseReportController::class, 'index'])->name('report');
    });
});
