<?php

use App\Http\Controllers\Admin\AuthController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Root — redirect ke beranda storefront
|--------------------------------------------------------------------------
*/
Route::get('/', fn () => redirect()->route('store.home'));

/*
|--------------------------------------------------------------------------
| Storefront Customer — public, tanpa auth
|--------------------------------------------------------------------------
*/
Route::prefix('store')->name('store.')->group(function () {
    Route::get('/',          \App\Livewire\Store\Home::class)->name('home');
    Route::get('/produk',    \App\Livewire\Store\Products::class)->name('products');
    Route::get('/produk/{slug}', \App\Livewire\Store\ProductShow::class)->name('products.show');
    Route::get('/mitra',     \App\Livewire\Store\Partners::class)->name('partners');
    Route::get('/mitra/{id}', \App\Livewire\Store\PartnerShow::class)->name('partners.show');
    Route::get('/keranjang', \App\Livewire\Store\Cart::class)->name('cart');
    Route::post('/keranjang/tambah', [\App\Http\Controllers\Store\CartController::class, 'add'])->name('cart.add');
    Route::get('/keranjang/jumlah', [\App\Http\Controllers\Store\CartController::class, 'count'])->name('cart.count');
    Route::get('/checkout', \App\Livewire\Store\Checkout::class)->name('checkout');
    Route::get('/pesanan',              \App\Livewire\Store\Orders::class)->name('orders');
    Route::get('/pesanan/{orderNumber}', \App\Livewire\Store\OrderShow::class)->name('orders.show');
    Route::get('/akun',                  \App\Livewire\Store\Account::class)->name('account');
    Route::get('/tentang',               \App\Livewire\Store\InfoPage::class)->name('info.about')->defaults('page', 'about');
    Route::get('/hubungi-kami',          \App\Livewire\Store\InfoPage::class)->name('info.contact')->defaults('page', 'contact');
    Route::get('/faq',                   \App\Livewire\Store\InfoPage::class)->name('info.faq')->defaults('page', 'faq');
    Route::get('/kebijakan-privasi',     \App\Livewire\Store\InfoPage::class)->name('info.privacy')->defaults('page', 'privacy');
    Route::get('/syarat-ketentuan',      \App\Livewire\Store\InfoPage::class)->name('info.terms')->defaults('page', 'terms');
});

/*
|--------------------------------------------------------------------------
| Admin — Auth routes (guest only)
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->middleware('guest')->group(function () {
    Route::get('/login',  [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.post');
});

/*
|--------------------------------------------------------------------------
| Admin — Logout
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->middleware('auth')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
});

/*
|--------------------------------------------------------------------------
| Admin — Panel
|
| Middleware:
|   auth       -> pastikan sudah login
|   active     -> pastikan akun aktif
|   admin.role -> pastikan punya role admin/super_admin (pakai hasAnyRole,
|                 tidak lewat Gate — agar admin dan super_admin diperlakukan sama)
|--------------------------------------------------------------------------
*/
Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth', 'active', 'admin.role'])
    ->group(function () {

        Route::get('/',        \App\Livewire\Admin\Dashboard::class)->name('dashboard');
        Route::get('/profile', \App\Livewire\Admin\Profile::class)->name('profile');

        // Hanya super_admin — pakai admin.role dengan parameter eksplisit
        Route::middleware('admin.role:super_admin')->group(function () {
            Route::get('/roles',            \App\Livewire\Admin\RolePermission::class)->name('roles');
            Route::get('/manajemen-admin',  \App\Livewire\Admin\ManajemenAdmin\Index::class)->name('manajemen-admin');
            Route::get('/pengaturan',       \App\Livewire\Admin\Pengaturan\Index::class)->name('pengaturan');
            Route::get('/aktivitas',        \App\Livewire\Admin\Aktivitas\Index::class)->name('aktivitas');
            Route::get('/maintenance',      \App\Livewire\Admin\Maintenance\Index::class)->name('maintenance');
        });

        Route::get('/kategori',         \App\Livewire\Admin\Kategori\Index::class)->name('kategori');
        Route::get('/mitra',            \App\Livewire\Admin\Mitra\Index::class)->name('mitra');
        Route::get('/produk',           \App\Livewire\Admin\Produk\Index::class)->name('produk');
        Route::get('/produk/tambah',    \App\Livewire\Admin\Produk\Form::class)->name('produk.create');
        Route::get('/produk/{id}/edit', \App\Livewire\Admin\Produk\Form::class)->name('produk.edit');
        Route::get('/banner',           \App\Livewire\Admin\Banner\Index::class)->name('banner');
        Route::get('/voucher',          \App\Livewire\Admin\Voucher\Index::class)->name('voucher');

        Route::get('/customer',             \App\Livewire\Admin\Customer\Index::class)->name('customer');
        Route::get('/customer/{type}/{id}', \App\Livewire\Admin\Customer\Show::class)->name('customer.show');

        Route::get('/pesanan',                \App\Livewire\Admin\Pesanan\Index::class)->name('pesanan');
        Route::get('/pesanan/{id}',            \App\Livewire\Admin\Pesanan\Show::class)->name('pesanan.show');
        Route::get('/pesanan/{id}/invoice', [\App\Http\Controllers\Admin\PesananController::class, 'invoice'])->name('pesanan.invoice');

        Route::get('/pembayaran',     \App\Livewire\Admin\Pembayaran\Index::class)->name('pembayaran');
        Route::get('/pembayaran/{id}', \App\Livewire\Admin\Pembayaran\Show::class)->name('pembayaran.show');
        Route::get('/ongkir',     \App\Livewire\Admin\Ongkir\Index::class)->name('ongkir');
        Route::get('/laporan',        \App\Livewire\Admin\Laporan\Index::class)->name('laporan');
        Route::get('/laporan/export', [\App\Http\Controllers\Admin\LaporanController::class, 'export'])->name('laporan.export');

    });
