<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\TrackingController;

// Public/Customer Routes
Route::get('/', [TrackingController::class, 'index'])->name('tracking.index');
Route::get('/tentang', [TrackingController::class, 'about'])->name('about');
Route::get('/api/mitra', [\App\Http\Controllers\SettingController::class, 'mitra'])
    ->name('mitra.public')
    ->middleware('throttle:60,1');
Route::get('/api/banners', [\App\Http\Controllers\BannerController::class, 'publicIndex'])
    ->name('banners.public')
    ->middleware('throttle:60,1');
Route::get('/api/track/{receipt_number}', [TrackingController::class, 'track'])
    ->name('tracking.api')
    ->middleware('throttle:30,1'); // Max 30 requests per minute per IP

// Guest/Auth Routes
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});

// Authenticated/Admin Routes
Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
    Route::get('/admin', [OrderController::class, 'adminDashboard'])->name('admin.dashboard');

    // Customer AJAX CRUD Routes
    Route::prefix('admin/customers')->name('admin.customers.')->group(function () {
        Route::get('/', [CustomerController::class, 'index'])->name('index');
        Route::get('/{id}/history', [CustomerController::class, 'history'])->name('history');
        Route::post('/', [CustomerController::class, 'store'])->name('store');
        Route::delete('/{id}', [CustomerController::class, 'destroy'])->name('destroy');
    });

    // Courier AJAX CRUD Routes
    Route::prefix('admin/couriers')->name('admin.couriers.')->group(function () {
        Route::get('/', [\App\Http\Controllers\CourierController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\CourierController::class, 'store'])->name('store');
        Route::delete('/{id}', [\App\Http\Controllers\CourierController::class, 'destroy'])->name('destroy');
    });

    // Banner CRUD Routes
    Route::prefix('admin/banners')->name('admin.banners.')->group(function () {
        Route::get('/', [\App\Http\Controllers\BannerController::class, 'index'])->name('index');
        Route::post('/', [\App\Http\Controllers\BannerController::class, 'store'])->name('store');
        Route::delete('/{id}', [\App\Http\Controllers\BannerController::class, 'destroy'])->name('destroy');
    });

    // Branding settings (logo + favicon)
    Route::prefix('admin/settings')->name('admin.settings.')->group(function () {
        Route::get('/', [\App\Http\Controllers\SettingController::class, 'index'])->name('index');
        Route::post('/logo', [\App\Http\Controllers\SettingController::class, 'uploadLogo'])->name('logo.upload');
        Route::delete('/logo', [\App\Http\Controllers\SettingController::class, 'deleteLogo'])->name('logo.delete');
        Route::post('/favicon', [\App\Http\Controllers\SettingController::class, 'uploadFavicon'])->name('favicon.upload');
        Route::delete('/favicon', [\App\Http\Controllers\SettingController::class, 'deleteFavicon'])->name('favicon.delete');
        Route::get('/mitra', [\App\Http\Controllers\SettingController::class, 'mitra'])->name('mitra');
        Route::put('/mitra', [\App\Http\Controllers\SettingController::class, 'updateMitra'])->name('mitra.update');
    });

    // Admin profile
    Route::prefix('admin/profile')->name('admin.profile.')->group(function () {
        Route::get('/', [\App\Http\Controllers\ProfileController::class, 'index'])->name('index');
        Route::put('/', [\App\Http\Controllers\ProfileController::class, 'update'])->name('update');
        Route::put('/password', [\App\Http\Controllers\ProfileController::class, 'updatePassword'])->name('password.update');
    });

    // Order AJAX CRUD Routes
    Route::prefix('admin/orders')->name('admin.orders.')->group(function () {
        Route::get('/', [OrderController::class, 'index'])->name('index');
        Route::get('/generate-receipt', [OrderController::class, 'generateReceipt'])->name('generate-receipt');
        Route::get('/{id}/receipt', [OrderController::class, 'receipt'])->name('receipt');
        Route::post('/', [OrderController::class, 'store'])->name('store');
        Route::delete('/{id}', [OrderController::class, 'destroy'])->name('destroy');
    });
});

