<?php

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

// ─── Admin Auth ─────────────────────────────────────────────────────────────
Route::prefix('admin')->name('admin.')->group(function () {

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

    Route::post('/logout', [Admin\AuthController::class, 'logout'])
        ->middleware('auth:admin')
        ->name('logout');

    // ─── Protected Admin Routes ──────────────────────────────────────────────
    Route::middleware('auth:admin')->group(function () {

        // Dashboard
        Route::get('/', [Admin\DashboardController::class, 'index'])->name('dashboard');
        Route::get('/dashboard', [Admin\DashboardController::class, 'index']);

        // Customers
        Route::prefix('customers')->name('customers.')->group(function () {
            Route::get('/', [Admin\CustomerController::class, 'index'])->name('index');
            Route::get('/create', [Admin\CustomerController::class, 'create'])->name('create');
            Route::post('/', [Admin\CustomerController::class, 'store'])->name('store');
            Route::get('/{customer}', [Admin\CustomerController::class, 'show'])->name('show');
            Route::post('/{customer}/block', [Admin\CustomerController::class, 'block'])->name('block');
            Route::post('/{customer}/unblock', [Admin\CustomerController::class, 'unblock'])->name('unblock');
        });

        // Vendors
        Route::prefix('vendors')->name('vendors.')->group(function () {
            Route::get('/', [Admin\VendorController::class, 'index'])->name('index');
            Route::get('/create', [Admin\VendorController::class, 'create'])->name('create');
            Route::post('/', [Admin\VendorController::class, 'store'])->name('store');
            Route::get('/{vendor}', [Admin\VendorController::class, 'show'])->name('show');
            Route::post('/{vendor}/approve', [Admin\VendorController::class, 'approve'])->name('approve');
            Route::post('/{vendor}/reject', [Admin\VendorController::class, 'reject'])->name('reject');
            Route::post('/{vendor}/suspend', [Admin\VendorController::class, 'suspend'])->name('suspend');
            Route::post('/{vendor}/reactivate', [Admin\VendorController::class, 'reactivate'])->name('reactivate');
            Route::post('/{vendor}/assign-category', [Admin\VendorController::class, 'assignCategory'])->name('assign-category');
            Route::post('/{vendor}/assign-services', [Admin\VendorController::class, 'assignServices'])->name('assign-services');
            Route::post('/{vendor}/manual-kyc', [Admin\VendorController::class, 'manualKyc'])->name('manual-kyc');
        });

        // KYC
        Route::prefix('kyc')->name('kyc.')->group(function () {
            Route::get('/', [Admin\KycController::class, 'index'])->name('index');
            Route::get('/vendor/{vendor}', [Admin\KycController::class, 'show'])->name('show');
            Route::post('/documents/{document}/approve', [Admin\KycController::class, 'approveDocument'])->name('document.approve');
            Route::post('/documents/{document}/reject', [Admin\KycController::class, 'rejectDocument'])->name('document.reject');
            Route::post('/vendor/{vendor}/approve-all', [Admin\KycController::class, 'approveAll'])->name('approve-all');
            Route::post('/vendor/{vendor}/reject-all', [Admin\KycController::class, 'rejectAll'])->name('reject-all');
        });

        // Vendor Types (Phase 2)
        Route::get('vendor-types', [Admin\VendorTypeController::class, 'index'])->name('vendor-types.index');
        Route::get('vendor-types/create', [Admin\VendorTypeController::class, 'create'])->name('vendor-types.create');
        Route::post('vendor-types', [Admin\VendorTypeController::class, 'store'])->name('vendor-types.store');
        Route::get('vendor-types/{vendorType}/edit', [Admin\VendorTypeController::class, 'edit'])->name('vendor-types.edit');
        Route::put('vendor-types/{vendorType}', [Admin\VendorTypeController::class, 'update'])->name('vendor-types.update');
        Route::post('vendor-types/{vendorType}/toggle', [Admin\VendorTypeController::class, 'toggle'])->name('vendor-types.toggle');

        // Provider Services (Phase 2)
        Route::get('provider-services', [Admin\ProviderServiceController::class, 'index'])->name('provider-services.index');
        Route::get('provider-services/create', [Admin\ProviderServiceController::class, 'create'])->name('provider-services.create');
        Route::post('provider-services', [Admin\ProviderServiceController::class, 'store'])->name('provider-services.store');
        Route::get('provider-services/{providerService}/edit', [Admin\ProviderServiceController::class, 'edit'])->name('provider-services.edit');
        Route::put('provider-services/{providerService}', [Admin\ProviderServiceController::class, 'update'])->name('provider-services.update');
        Route::post('provider-services/{providerService}/toggle', [Admin\ProviderServiceController::class, 'toggle'])->name('provider-services.toggle');
        Route::delete('provider-services/{providerService}', [Admin\ProviderServiceController::class, 'destroy'])->name('provider-services.destroy');

        // Wallet
        Route::prefix('wallet')->name('wallet.')->group(function () {
            Route::get('/', [Admin\WalletController::class, 'index'])->name('index');
            Route::post('/credit', [Admin\WalletController::class, 'credit'])->name('credit');
            Route::post('/debit', [Admin\WalletController::class, 'debit'])->name('debit');
            Route::get('/withdrawals', [Admin\WalletController::class, 'withdrawals'])->name('withdrawals');
            Route::post('/withdrawals/{transaction}/approve', [Admin\WalletController::class, 'approveWithdrawal'])->name('withdrawals.approve');
            Route::post('/withdrawals/{transaction}/reject', [Admin\WalletController::class, 'rejectWithdrawal'])->name('withdrawals.reject');
        });

        // Ads
        Route::prefix('ads')->name('ads.')->group(function () {
            Route::get('/', [Admin\AdController::class, 'index'])->name('index');
            Route::get('/{ad}', [Admin\AdController::class, 'show'])->name('show');
            Route::post('/{ad}/approve', [Admin\AdController::class, 'approve'])->name('approve');
            Route::post('/{ad}/reject', [Admin\AdController::class, 'reject'])->name('reject');
            Route::delete('/{ad}', [Admin\AdController::class, 'destroy'])->name('destroy');
        });

        // Orders
        Route::prefix('orders')->name('orders.')->group(function () {
            Route::get('/', [Admin\OrderController::class, 'index'])->name('index');
            Route::get('/{order}', [Admin\OrderController::class, 'show'])->name('show');
            Route::post('/{order}/cancel', [Admin\OrderController::class, 'cancel'])->name('cancel');
            Route::post('/{order}/resolve-dispute', [Admin\OrderController::class, 'resolveDispute'])->name('resolve-dispute');
        });

        // Phase 2: Service Provider Admin Module
        Route::prefix('service-requests')->name('service-requests.')->group(function () {
            Route::get('/', [Admin\ServiceRequestController::class, 'index'])->name('index');
            Route::get('/{service_request}', [Admin\ServiceRequestController::class, 'show'])->name('show');
        });

        // Subscriptions
        Route::resource('subscriptions', Admin\SubscriptionController::class);

        // Settings (Finance/Super Admin only)
        Route::middleware('role:super_admin,finance')->group(function () {
            Route::get('/settings', [Admin\SettingsController::class, 'index'])->name('settings.index');
            Route::post('/settings', [Admin\SettingsController::class, 'update'])->name('settings.update');
        });

        // Notifications
        Route::prefix('notifications')->name('notifications.')->group(function () {
            Route::get('/', [Admin\NotificationController::class, 'index'])->name('index');
            Route::get('/create', [Admin\NotificationController::class, 'create'])->name('create');
            Route::post('/', [Admin\NotificationController::class, 'store'])->name('store');
        });
    });
});
