<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{
    HomeController, AuthController, DashboardController, 
    CartController, OrderController, JasaController, 
    AddressController, ProfileController, SettingController,
    CategoryController, ProductController, ReviewController,
    PasswordResetController
};

// --- PUBLIC ROUTES ---
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/jasa/preview/{id}', [JasaController::class, 'previewMedia'])->name('jasa.preview');

// --- AUTH GUEST ---
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']);

    // Password Reset
    Route::get('/forgot-password', [PasswordResetController::class, 'showLinkRequestForm'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetController::class, 'sendResetLinkEmail'])->name('password.email');
    Route::get('/reset-password/{token}', [PasswordResetController::class, 'showResetForm'])->name('password.reset');
    Route::post('/reset-password', [PasswordResetController::class, 'reset'])->name('password.update');
});

// --- PROTECTED ROUTES (LOGGED IN) ---
// --- PROTECTED ROUTES (LOGGED IN) ---
Route::middleware(['auth'])->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

    // ==========================================
    // --- ADMIN ROUTES (Role: admin) ---
    // ==========================================
    Route::middleware(['role:admin'])->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
        
        // Settings & General Admin
        Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');
        
        // Admin Jasa
        Route::get('/service/admin/list', [JasaController::class, 'adminIndex'])->name('admin.jasa.index');
        Route::post('/service/update-status/{id}', [JasaController::class, 'updateStatus'])->name('jasa.status.update');
        Route::delete('/service/delete/{id}', [JasaController::class, 'destroy'])->name('jasa.delete');
        
        // Admin Orders
        Route::get('/admin/orders', [OrderController::class, 'adminIndex'])->name('admin.orders.index');
        Route::patch('/admin/orders/{id}/status', [OrderController::class, 'adminUpdateStatus'])->name('admin.orders.status');

        // Resources (Admin ONLY can manage: Create, Edit, Update, Delete)
        Route::resource('categories', CategoryController::class)->except(['show']);
        Route::resource('products', ProductController::class)->except(['show']);
        Route::get('/stock-history', [ProductController::class, 'stockHistory'])->name('products.stock.history');
        Route::get('/products/{id}/stock', [ProductController::class, 'stockDetail'])->name('products.stock.detail');
        Route::post('/products/{id}/adjust-stock', [ProductController::class, 'adjustStock'])->name('products.stock.adjust');
    });

    // Move public product detail here to avoid conflict with admin routes (products/create)
    Route::get('/products/{product:slug}', [ProductController::class, 'show'])->name('products.show');


    // ==========================================
    // --- PELANGGAN ROUTES (Role: pelanggan) ---
    // ==========================================
    Route::middleware(['role:pelanggan'])->group(function () {
        // JASA SERVICE
        Route::prefix('service')->group(function () {
            Route::get('/list', [JasaController::class, 'index'])->name('jasa.index');
            Route::get('/form', [JasaController::class, 'create'])->name('jasa.create');
            Route::post('/kirim', [JasaController::class, 'kirimWa'])->name('jasa.kirim');
            Route::post('/rating/{id}', [JasaController::class, 'submitRating'])->name('jasa.rating');
        });

        // ALAMAT & PROFIL
        Route::get('/alamat/tambah', [AddressController::class, 'create'])->name('addresses.create');
        Route::post('/address/store', [AddressController::class, 'store'])->name('address.store');
        Route::delete('/address/{id}', [AddressController::class, 'destroy'])->name('address.delete');
        Route::get('/api/address/cards', [AddressController::class, 'getCards'])->name('address.cards');
        Route::put('/profile/update', [ProfileController::class, 'update'])->name('profile.update');

        // KERANJANG & CHECKOUT
        Route::controller(CartController::class)->group(function () {
            Route::get('/cart', 'index')->name('cart.index');
            Route::post('/cart/add/{id}', 'add')->name('cart.add');
            Route::delete('/cart/remove/{id}', 'destroy')->name('cart.remove');
            Route::post('/cart/checkout', 'checkout')->name('cart.checkout');
        });

        Route::controller(OrderController::class)->group(function () {
            Route::post('/buy-now/{id}', 'buyNow')->name('buy.now');
            Route::post('/checkout', 'store')->name('checkout.store');
            Route::get('/my-orders', 'myOrders')->name('orders.history');
            Route::get('/my-orders/{id}', 'myOrderDetail')->name('orders.detail');
            Route::post('/my-orders/{id}/cancel', 'cancel')->name('orders.cancel');
            Route::get('/order/success/{id}', 'success')->name('order.success');
            Route::post('/order/update-status/{id}', 'updateStatus')->name('order.update-status');
        });

        // REVIEWS
        Route::post('/reviews', [ReviewController::class, 'store'])->name('reviews.store');
    });

    // API wilayah (Bisa diakses keduanya tetapi di dalam auth)
    Route::get('/api/regencies/{province_id}', [AddressController::class, 'getRegencies']);
    Route::get('/api/districts/{regency_id}', [AddressController::class, 'getDistricts']);
    Route::get('/api/villages/{district_id}', [AddressController::class, 'getVillages']);
});