<?php

use App\Http\Controllers\AdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// Public routes
Route::get('/', function () {
    return redirect()->route('menu.index');
});

// Authentication routes
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// User/Menu routes (accessible by authenticated users)
Route::middleware(['auth'])->group(function () {
    Route::get('/menu', [UserController::class, 'index'])->name('menu.index');
    Route::post('/menu/add-to-cart', [UserController::class, 'addToCart'])->name('menu.add-to-cart');
    Route::get('/menu/cart', [UserController::class, 'cart'])->name('menu.cart');
    Route::put('/menu/cart/{orderItem}', [UserController::class, 'updateCart'])->name('menu.update-cart');
    Route::delete('/menu/cart/{orderItem}', [UserController::class, 'removeFromCart'])->name('menu.remove-from-cart');
    Route::post('/menu/order/{order}/submit', [UserController::class, 'submitOrder'])->name('menu.submit-order');
    Route::delete('/menu/order/{order}/cancel', [UserController::class, 'cancelOrder'])->name('menu.cancel-order');
    Route::get('/menu/orders', [UserController::class, 'orders'])->name('menu.orders');
    Route::get('/menu/orders/{order}', [UserController::class, 'showOrder'])->name('menu.order-detail');
    Route::get('/menu/orders/{order}/qris', [UserController::class, 'showQrisPayment'])->name('menu.qris-payment');
});

// Admin routes (accessible by admin only)
Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('/admin/qrcode', [AdminController::class, 'qrcode'])->name('admin.qrcode');
    Route::post('/admin/qrcode', [AdminController::class, 'updateQris'])->name('admin.qrcode.update');
    
    // Category management
    Route::get('/admin/categories', [AdminController::class, 'categories'])->name('admin.categories');
    Route::post('/admin/categories', [AdminController::class, 'storeCategory'])->name('admin.categories.store');
    Route::put('/admin/categories/{category}', [AdminController::class, 'updateCategory'])->name('admin.categories.update');
    Route::delete('/admin/categories/{category}', [AdminController::class, 'destroyCategory'])->name('admin.categories.destroy');
    
    // Menu management
    Route::get('/admin/menus', [AdminController::class, 'menus'])->name('admin.menus');
    Route::post('/admin/menus', [AdminController::class, 'storeMenu'])->name('admin.menus.store');
    Route::put('/admin/menus/{menu}', [AdminController::class, 'updateMenu'])->name('admin.menus.update');
    Route::delete('/admin/menus/{menu}', [AdminController::class, 'destroyMenu'])->name('admin.menus.destroy');
    
    // Order management
    Route::put('/admin/orders/{order}/status', [AdminController::class, 'updateOrderStatus'])->name('admin.orders.update-status');
    Route::delete('/admin/orders/{order}', [AdminController::class, 'destroyOrder'])->name('admin.orders.destroy');
});
