<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\{
    AuthController,
    CategoryController,
    ProductController,
    DashboardController,
    StockLogController,
    CartController,
    PaymentController,
    AddressController,
    OrderController,
    ReviewController,
    MidtransWebhookController,
    ProfileController,
};

// 1. HALAMAN UTAMA
// 1. HALAMAN UTAMA
Route::get('/', function () {
    if (Auth::check()) {
        $role = Auth::user()->role;

        if ($role === 'admin') {
            return redirect()->route('admin.dashboard');
        } elseif ($role === 'courier') {
            return redirect()->route('courier.dashboard');
        } else {
            return redirect()->route('user.dashboard');
        }
    }

    $products = \App\Models\Product::with('category')->latest()->take(6)->get();
    return view('welcome', compact('products'));
})->name('welcome');
// 2. GUEST AREA
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.process');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.process');
    Route::post('/send-code', [AuthController::class, 'sendVerificationCode'])->name('send.code');
});

// 3. MIDTRANS WEBHOOK
Route::post('/midtrans/webhook', [MidtransWebhookController::class, 'handle'])
    ->name('midtrans.webhook');

// 4. WILAYAH
Route::prefix('indoregion')->group(function () {
    Route::get('/regencies/{code}', function ($code) {
        return \Laravolt\Indonesia\Models\City::where('province_code', $code)
            ->select('code', 'name')->orderBy('name')->get();
    });
    Route::get('/districts/{code}', function ($code) {
        return \Laravolt\Indonesia\Models\District::where('city_code', $code)
            ->select('code', 'name')->orderBy('name')->get();
    });
    Route::get('/villages/{code}', function ($code) {
        return \Laravolt\Indonesia\Models\Village::where('district_code', $code)
            ->select('code', 'name')->orderBy('name')->get();
    });
});

// 5. AUTH AREA
Route::middleware('auth')->group(function () {

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

    // =========================================================
    // ROLE: USER
    // =========================================================
    Route::middleware('role:user')->prefix('user')->name('user.')->group(function () {

        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::get('/products', [ProductController::class, 'index'])->name('products.index');
        Route::get('/products/{product}', [ProductController::class, 'show'])->name('products.show');
        Route::post('/calculate-ongkir', [ProductController::class, 'calculateOngkir'])->name('calculate.ongkir');

        // Alamat
        Route::get('/address/create', [AddressController::class, 'create'])->name('address.create');
        Route::post('/address/store', [AddressController::class, 'store'])->name('address.store');
        Route::get('/address/{address}/edit', [AddressController::class, 'edit'])->name('address.edit');
        Route::put('/address/{address}', [AddressController::class, 'update'])->name('address.update');
        Route::delete('/address/{address}', [AddressController::class, 'destroy'])->name('address.destroy');
        Route::post('/address/calculate-shipping', [AddressController::class, 'calculateShipping'])
            ->name('address.calculate.shipping');

        // Cart
        Route::prefix('cart')->name('cart.')->group(function () {
            Route::get('/', [CartController::class, 'index'])->name('index');
            Route::post('/add/{id}', [CartController::class, 'add'])->name('add');
            Route::get('/buy-now/{id}', [CartController::class, 'buyNow'])->name('buy.now');
            Route::patch('/update', [CartController::class, 'update'])->name('update');
            Route::delete('/remove', [CartController::class, 'remove'])->name('remove');
            Route::get('/checkout/{id?}', [CartController::class, 'checkout'])->name('checkout');
        });

        Route::post('/payment/process', [PaymentController::class, 'process'])->name('payment.process');

        // Pesanan milik user
        Route::post('/orders/{order}/cancel', [OrderController::class, 'cancelByUser'])->name('orders.cancel');
        Route::prefix('my-orders')->name('orders.')->group(function () {
            Route::get('/', [OrderController::class, 'index'])->name('index');
            Route::get('/{order}', [OrderController::class, 'show'])->name('show');
        });

        // Review
        Route::get('/orders/{order}/review', [ReviewController::class, 'create'])->name('reviews.create');
        Route::post('/orders/{order}/review', [ReviewController::class, 'store'])->name('reviews.store');

        // Profile
        Route::get('/profile', [App\Http\Controllers\User\ProfileController::class, 'edit'])->name('profile.edit');
        Route::put('/profile', [App\Http\Controllers\User\ProfileController::class, 'update'])->name('profile.update');
    });

    // =========================================================
    // ROLE: ADMIN
    // =========================================================
    Route::middleware('role:admin')->prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

        Route::get('/profile/address', [App\Http\Controllers\Admin\ProfileController::class, 'editAddress'])->name('address.edit');
        Route::put('/profile/address', [App\Http\Controllers\Admin\ProfileController::class, 'updateAddress'])->name('address.update');

        Route::resource('categories', CategoryController::class);
        Route::resource('products', ProductController::class);

        Route::prefix('stock')->name('stock.')->group(function () {
            Route::get('/logs', [StockLogController::class, 'index'])->name('logs.index');
            Route::post('/logs/store', [StockLogController::class, 'store'])->name('logs.store');
            Route::get('/logs/export', [StockLogController::class, 'exportPDF'])->name('logs.export');
            Route::delete('/logs/{id}', [StockLogController::class, 'destroy'])->name('logs.destroy');
        });

        Route::prefix('orders')->name('orders.')->group(function () {
            Route::get('/', [OrderController::class, 'adminIndex'])->name('index');
            Route::patch('/{order}/cancel', [OrderController::class, 'cancel'])->name('cancel');
        });

        // Laporan Keuangan
        Route::prefix('financial')->name('financial.')->group(function () {
            Route::get('/',    [\App\Http\Controllers\Admin\FinancialReportController::class, 'index'])->name('index');
            Route::get('/pdf', [\App\Http\Controllers\Admin\FinancialReportController::class, 'downloadPDF'])->name('pdf');
        });

        // Gaji Kurir
        Route::prefix('courier-salary')->name('courier-salary.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\CourierSalaryController::class, 'index'])->name('index');
            Route::patch('/{salary}/pay', [\App\Http\Controllers\Admin\CourierSalaryController::class, 'markPaid'])->name('pay');
        });
    });

    // =========================================================
    // ROLE: COURIER
    // =========================================================
    Route::middleware('role:courier')->prefix('courier')->name('courier.')->group(function () {
        Route::get('/dashboard', [OrderController::class, 'courierIndex'])->name('dashboard');
        Route::post('/orders/{order}/pickup', [OrderController::class, 'pickUp'])->name('orders.pickup');
        Route::post('/orders/{order}/start', [OrderController::class, 'startDelivery'])->name('orders.start');
        Route::post('/orders/{order}/complete', [OrderController::class, 'completeOrder'])->name('orders.complete');

        // Gaji kurir (lihat sendiri)
        Route::get('/salary', [\App\Http\Controllers\Admin\CourierSalaryController::class, 'courierIndex'])->name('salary');
    });
});