<?php

use App\Http\Controllers\AdminController; 
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\MenuController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\MejaController;
use App\Http\Controllers\TransaksiController;
use App\Http\Controllers\RiwayatController;
use App\Http\Controllers\KritikSaranController;
use App\Http\Controllers\ReservasiController;
use App\Http\Controllers\Admin\MenuController as AdminMenuController;
use App\Http\Controllers\Admin\TableController;
use App\Http\Controllers\Admin\ReservationController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserProfileController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\LandingPageController;
use App\Http\Controllers\Admin\GalleryController;
use App\Http\Controllers\BestsellerController;

// Authentication Routes
Route::middleware('guest')->group(function () {
    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']);
    
    // Forgot Password Routes
    Route::get('forgot-password', [AuthController::class, 'showForgotPassword'])->name('password.request');
    Route::post('forgot-password', [AuthController::class, 'sendResetLink'])->name('password.email');
    Route::get('reset-password/{token}', [AuthController::class, 'showResetPassword'])->name('password.reset');
    Route::post('reset-password', [AuthController::class, 'resetPassword'])->name('password.update');
});

// Admin Routes
Route::group(['middleware' => ['auth', 'admin'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
    Route::get('/chart-data', [AdminController::class, 'ajaxChartData'])->name('chart-data');
    
    // Menu Management Routes
    Route::get('/menu', [AdminMenuController::class, 'index'])->name('menu');
    Route::post('/menu', [AdminMenuController::class, 'store'])->name('menu.store');
    Route::put('/menu/{menuItem}', [AdminMenuController::class, 'update'])->name('menu.update');
    Route::delete('/menu/{menuItem}', [AdminMenuController::class, 'destroy'])->name('menu.destroy');

    // Table Management Routes
    Route::get('/tables', [TableController::class, 'index'])->name('tables');
    Route::post('/tables', [TableController::class, 'store'])->name('tables.store');
    Route::put('/tables/{table}', [TableController::class, 'update'])->name('tables.update');
    Route::delete('/tables/{table}', [TableController::class, 'destroy'])->name('tables.destroy');
    Route::get('/tables/{table}/schedule', [TableController::class, 'getSchedule'])->name('tables.schedule');

    // Reservation Management Routes
    Route::get('/reservations', [ReservationController::class, 'index'])->name('reservations');
    Route::post('/reservations', [ReservationController::class, 'store'])->name('reservations.store');
    Route::get('/reservations/time-slots', [ReservationController::class, 'getAvailableTimeSlots'])->name('reservations.time-slots');
    Route::put('/reservations/{reservation}', [ReservationController::class, 'update'])->name('reservations.update');
    Route::delete('/reservations/{reservation}', [ReservationController::class, 'destroy'])->name('reservations.destroy');
    Route::get('/tables/{meja}/schedule', [ReservationController::class, 'getSchedule']);

    // Carousel Management Routes
    Route::get('/carousel', [App\Http\Controllers\Admin\CarouselController::class, 'index'])->name('carousel.index');
    Route::post('/carousel', [App\Http\Controllers\Admin\CarouselController::class, 'store'])->name('carousel.store');
    Route::put('/carousel/{carousel}', [App\Http\Controllers\Admin\CarouselController::class, 'update'])->name('carousel.update');
    Route::delete('/carousel/{carousel}', [App\Http\Controllers\Admin\CarouselController::class, 'destroy'])->name('carousel.destroy');
    Route::post('/carousel/reorder', [App\Http\Controllers\Admin\CarouselController::class, 'reorder'])->name('carousel.reorder');

    // Gallery Routes
    Route::get('/gallery', [GalleryController::class, 'index'])->name('gallery.index');
    Route::get('/gallery/category/{category}', [GalleryController::class, 'getCategory'])->name('gallery.get-category');
    Route::get('/gallery/image/{image}', [GalleryController::class, 'getImage'])->name('gallery.get-image');
    Route::post('/gallery/category', [GalleryController::class, 'storeCategory'])->name('gallery.store-category');
    Route::put('/gallery/category/{category}', [GalleryController::class, 'updateCategory'])->name('gallery.update-category');
    Route::delete('/gallery/category/{category}', [GalleryController::class, 'destroyCategory'])->name('gallery.destroy-category');
    Route::post('/gallery/category/{category}/image', [GalleryController::class, 'storeImage'])->name('gallery.store-image');
    Route::put('/gallery/image/{image}', [GalleryController::class, 'updateImage'])->name('gallery.update-image');
    Route::delete('/gallery/image/{image}', [GalleryController::class, 'destroyImage'])->name('gallery.destroy-image');

    // Bestseller Routes
    Route::get('/bestseller', [BestsellerController::class, 'index'])->name('bestseller.index');
    Route::post('/bestseller', [BestsellerController::class, 'store'])->name('bestseller.store');
    Route::put('/bestseller/{bestseller}', [BestsellerController::class, 'update'])->name('bestseller.update');
    Route::delete('/bestseller/{bestseller}', [BestsellerController::class, 'destroy'])->name('bestseller.destroy');

    // Kritik & Saran Management
    Route::get('/kritiksaran', [App\Http\Controllers\Admin\KritikSaranController::class, 'index'])->name('kritiksaran.index');
    Route::delete('/kritiksaran/{id}', [App\Http\Controllers\Admin\KritikSaranController::class, 'destroy'])->name('kritiksaran.destroy');
});

// Public Routes
Route::get('/', [LandingPageController::class, 'index'])->name('landing');

// Midtrans Notification Handler (must be public)
Route::post('/transaksi/notification/handler', [TransaksiController::class, 'notification'])->name('transaksi.notification');

// Protected Routes
Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Profile Routes
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile.show');
    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password.update');
    Route::delete('/profile/picture', [ProfileController::class, 'deleteProfilePicture'])->name('profile.picture.delete');

    Route::get('/menu', [MenuController::class, 'index'])->name('menu.index');
    
    // Meja Routes
    Route::get('/meja', [MejaController::class, 'index'])->name('meja.index');
    Route::post('/meja/select', [MejaController::class, 'selectMeja'])->name('meja.select');
    Route::get('/meja/{id}/check-availability', [MejaController::class, 'checkAvailability'])->name('meja.checkAvailability');
    Route::get('/meja/next-available-dates', [MejaController::class, 'getNextAvailableDates'])->name('meja.nextAvailableDates');
    Route::post('/meja/{id}/update-status', [MejaController::class, 'updateStatus'])->name('meja.updateStatus');
    
    // Reservasi Routes
    Route::get('/reservasi', [ReservasiController::class, 'index'])->name('reservasi.index');
    Route::post('/reservasi', [ReservasiController::class, 'store'])->name('reservasi.store');
    Route::get('/reservasi/success/{id}', [ReservasiController::class, 'success'])->name('reservasi.success');
    Route::post('/reservasi/check-availability', [ReservasiController::class, 'checkAvailability'])->name('reservasi.check-availability');
    Route::get('/get-available-slots', [ReservasiController::class, 'getAvailableSlots'])->name('reservasi.getAvailableSlots');
    
    // Transaksi Routes
    Route::get('/transaksi', [TransaksiController::class, 'index'])->name('transaksi.index');
    Route::get('/transaksi/{id}', [TransaksiController::class, 'show'])->name('transaksi.show');
    Route::put('/transaksi/{transaksi}/cancel', [TransaksiController::class, 'cancel'])->name('transaksi.cancel');
    Route::get('/transaksi/{id}/detail', [TransaksiController::class, 'detail'])->name('transaksi.detail');
    Route::get('/transaksi/{id}/check-status', [TransaksiController::class, 'checkStatus'])->name('transaksi.check-status');
    Route::post('/transaksi/store', [TransaksiController::class, 'store'])->name('transaksi.store');
    Route::post('/transaksi/{id}/confirm', [TransaksiController::class, 'confirm'])->name('transaksi.confirm');
    
    // Midtrans Routes (for client side)
    Route::get('/transaksi/finish', [TransaksiController::class, 'finish'])->name('transaksi.finish');
    Route::get('/transaksi/unfinish', [TransaksiController::class, 'unfinish'])->name('transaksi.unfinish');
    Route::get('/transaksi/error', [TransaksiController::class, 'error'])->name('transaksi.error');
    
    Route::get('/riwayat', [RiwayatController::class, 'index'])->name('riwayat');
    Route::get('/kritiksaran', [KritikSaranController::class, 'index'])->name('kritiksaran');
    Route::post('/kritiksaran', [KritikSaranController::class, 'store'])->name('kritiksaran.store');
    
    // Cart Routes
    Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
    Route::post('/cart/add', [CartController::class, 'add'])->name('cart.add');
    Route::post('/cart/update', [CartController::class, 'update'])->name('cart.update');
    Route::post('/cart/remove', [CartController::class, 'remove'])->name('cart.remove');
    Route::post('/cart/clear', [CartController::class, 'clear'])->name('cart.clear');
    Route::get('/cart/get', [CartController::class, 'getCart'])->name('cart.get');
    
    // Checkout Routes
    Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');
    Route::post('/checkout/process', [CheckoutController::class, 'process'])->name('checkout.process');
    
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // User Profile Routes
    Route::get('/profile', [UserProfileController::class, 'show'])->name('user.profile');
    Route::put('/profile/update', [UserProfileController::class, 'updateProfile'])->name('user.profile.update');
    Route::put('/profile/password', [UserProfileController::class, 'updatePassword'])->name('user.password.update');
    Route::delete('/profile/picture', [UserProfileController::class, 'deleteProfilePicture'])->name('user.profile.picture.delete');
});
