<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/health', function () {
    return response()->json([
        'status' => 'ok',
        'timestamp' => now()->toIso8601String(),
    ]);
})->name('health');

Route::prefix('v1')->as('v1.')->group(function () {
    Route::post('/register', [\App\Http\Controllers\Api\V1\AuthController::class, 'register'])->name('register')->middleware('throttle:10,1');
    Route::post('/login', [\App\Http\Controllers\Api\V1\AuthController::class, 'login'])->name('login')->middleware('throttle:5,1');
    Route::post('/password/forgot', [\App\Http\Controllers\Api\V1\PasswordResetController::class, 'forgot'])->name('password.forgot')->middleware('throttle:5,1');
    Route::post('/password/verify-otp', [\App\Http\Controllers\Api\V1\PasswordResetController::class, 'verifyOtp'])->name('password.verify-otp')->middleware('throttle:10,1');
    Route::post('/password/reset', [\App\Http\Controllers\Api\V1\PasswordResetController::class, 'reset'])->name('password.reset')->middleware('throttle:5,1');

    Route::post('/payment/webhook', [\App\Http\Controllers\Api\V1\PaymentController::class, 'webhook'])->name('payment.webhook');
    /** GET autorisé : navigateur après paiement ou erreur de config (webhook ≠ callback). */
    Route::get('/payment/webhook', [\App\Http\Controllers\Api\V1\PaymentController::class, 'webhookGet'])->name('payment.webhook.get');
    /** Retour utilisateur après paiement (callback_url FedaPay) — pas le webhook. */
    Route::get('/payment/return', [\App\Http\Controllers\Api\V1\PaymentController::class, 'returnPage'])->name('payment.return');
    Route::get('/payment/simulate', [\App\Http\Controllers\Api\V1\PaymentController::class, 'simulate'])->name('payment.simulate');
    Route::get('/categories', [\App\Http\Controllers\Api\V1\CategoryController::class, 'index'])->name('categories.index');
    Route::get('/products', [\App\Http\Controllers\Api\V1\ProductController::class, 'index'])->name('products.index');
    Route::get('/products/{product}', [\App\Http\Controllers\Api\V1\ProductController::class, 'show'])->name('products.show');
    Route::get('/banners', [\App\Http\Controllers\Api\V1\BannerController::class, 'index'])->name('banners.index');
    Route::get('/banners/{banner}', [\App\Http\Controllers\Api\V1\BannerController::class, 'show'])->name('banners.show');
    
    Route::middleware('auth:sanctum')->group(function () {
       
        Route::post('/logout', [\App\Http\Controllers\Api\V1\AuthController::class, 'logout'])->name('logout');
        Route::get('/user', [\App\Http\Controllers\Api\V1\AuthController::class, 'user'])->name('user');
        Route::put('/user', [\App\Http\Controllers\Api\V1\AuthController::class, 'update'])->name('user.update');
        Route::put('/user/password', [\App\Http\Controllers\Api\V1\AuthController::class, 'updatePassword'])->name('user.password');

        Route::middleware('role:super_admin,admin')->group(function () {
            Route::post('/categories', [\App\Http\Controllers\Api\V1\CategoryController::class, 'store'])->name('categories.store');
            Route::get('/categories/{category}', [\App\Http\Controllers\Api\V1\CategoryController::class, 'show'])->name('categories.show');
            Route::put('/categories/{category}', [\App\Http\Controllers\Api\V1\CategoryController::class, 'update'])->name('categories.update');
            Route::delete('/categories/{category}', [\App\Http\Controllers\Api\V1\CategoryController::class, 'destroy'])->name('categories.destroy');

            Route::post('/products', [\App\Http\Controllers\Api\V1\ProductController::class, 'store'])->name('products.store');
            Route::put('/products/{product}', [\App\Http\Controllers\Api\V1\ProductController::class, 'update'])->name('products.update');
            Route::delete('/products/{product}', [\App\Http\Controllers\Api\V1\ProductController::class, 'destroy'])->name('products.destroy');
        });

        Route::get('/addresses', [\App\Http\Controllers\Api\V1\AddressController::class, 'index'])->name('addresses.index');
        Route::get('/addresses/{address}', [\App\Http\Controllers\Api\V1\AddressController::class, 'show'])->name('addresses.show');
        Route::post('/addresses', [\App\Http\Controllers\Api\V1\AddressController::class, 'store'])->name('addresses.store');
        Route::put('/addresses/{address}', [\App\Http\Controllers\Api\V1\AddressController::class, 'update'])->name('addresses.update');
        Route::delete('/addresses/{address}', [\App\Http\Controllers\Api\V1\AddressController::class, 'destroy'])->name('addresses.destroy');

        Route::get('/cart', [\App\Http\Controllers\Api\V1\CartController::class, 'index'])->name('cart.index');
        Route::post('/cart', [\App\Http\Controllers\Api\V1\CartController::class, 'store'])->name('cart.store');
        Route::put('/cart/{cartItem}', [\App\Http\Controllers\Api\V1\CartController::class, 'update'])->name('cart.update');
        Route::delete('/cart/{cartItem}', [\App\Http\Controllers\Api\V1\CartController::class, 'destroy'])->name('cart.destroy');

        // Emplettes - côté client
        Route::get('/emplettes', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'index'])->name('emplettes.index');
        Route::post('/emplettes', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'store'])->name('emplettes.store');
        Route::get('/emplettes/{emplette}', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'show'])->name('emplettes.show');
        Route::post('/emplettes/{emplette}/decision', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'clientDecision'])->name('emplettes.decision');
        Route::post('/emplettes/{emplette}/payment/init', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'initPayment'])->name('emplettes.payment.init');

        Route::get('/orders', [\App\Http\Controllers\Api\V1\OrderController::class, 'index'])->name('orders.index');
        Route::post('/orders', [\App\Http\Controllers\Api\V1\OrderController::class, 'store'])->name('orders.store');
        Route::get('/orders/{order}', [\App\Http\Controllers\Api\V1\OrderController::class, 'show'])->name('orders.show');
        Route::get('/orders/{order}/invoice-link', [\App\Http\Controllers\Api\V1\InvoiceController::class, 'createDownloadLink'])->name('orders.invoice-link');
        Route::get('/orders/{order}/invoice/download', [\App\Http\Controllers\Api\V1\InvoiceController::class, 'download'])->name('orders.invoice.download');
        Route::post('/orders/{order}/cancel', [\App\Http\Controllers\Api\V1\OrderController::class, 'cancel'])->name('orders.cancel');

        Route::post('/fcm/register', [\App\Http\Controllers\Api\V1\FcmController::class, 'register'])->name('fcm.register');
        Route::post('/fcm/unregister', [\App\Http\Controllers\Api\V1\FcmController::class, 'unregister'])->name('fcm.unregister');
        Route::get('/notifications', [\App\Http\Controllers\Api\V1\NotificationController::class, 'index'])->name('notifications.index');
        Route::patch('/notifications/{id}/read', [\App\Http\Controllers\Api\V1\NotificationController::class, 'markAsRead'])->name('notifications.read');
        Route::post('/notifications/read-all', [\App\Http\Controllers\Api\V1\NotificationController::class, 'markAllAsRead'])->name('notifications.read-all');

        Route::post('/payment/init', [\App\Http\Controllers\Api\V1\PaymentController::class, 'init'])->name('payment.init');
        Route::get('/payment/orders/{order}/status', [\App\Http\Controllers\Api\V1\PaymentController::class, 'verify'])->name('payment.verify');

        Route::get('/favorites/ids', [\App\Http\Controllers\Api\V1\FavoriteController::class, 'ids'])->name('favorites.ids');
        Route::get('/favorites', [\App\Http\Controllers\Api\V1\FavoriteController::class, 'index'])->name('favorites.index');
        Route::post('/favorites', [\App\Http\Controllers\Api\V1\FavoriteController::class, 'store'])->name('favorites.store');
        Route::delete('/favorites/{product}', [\App\Http\Controllers\Api\V1\FavoriteController::class, 'destroy'])->name('favorites.destroy');

        // Livraisons
        Route::middleware('role:super_admin,admin')->group(function () {
            Route::get('/admin/dashboard', \App\Http\Controllers\Api\V1\DashboardController::class)->name('admin.dashboard');

            Route::get('/admin/activity-logs', [\App\Http\Controllers\Api\V1\ActivityLogController::class, 'index'])->name('admin.activity-logs.index');
            Route::get('/admin/orders/export', [\App\Http\Controllers\Api\V1\OrderExportController::class, 'csv'])->name('admin.orders.export');
        });

        Route::middleware('role:super_admin')->group(function () {
            Route::get('/admin/admins', [\App\Http\Controllers\Api\V1\AdminController::class, 'index'])->name('admin.admins.index');
            Route::post('/admin/admins', [\App\Http\Controllers\Api\V1\AdminController::class, 'store'])->name('admin.admins.store');
            Route::put('/admin/admins/{admin}', [\App\Http\Controllers\Api\V1\AdminController::class, 'update'])->name('admin.admins.update');
            Route::delete('/admin/admins/{admin}', [\App\Http\Controllers\Api\V1\AdminController::class, 'destroy'])->name('admin.admins.destroy');
        });

        Route::middleware('role:super_admin,admin')->group(function () {

            Route::get('/deliveries', [\App\Http\Controllers\Api\V1\DeliveryController::class, 'index'])->name('deliveries.index');
            Route::post('/deliveries/{order}/assign', [\App\Http\Controllers\Api\V1\DeliveryController::class, 'assign'])->name('deliveries.assign');
            Route::get('/deliveries/livreurs', [\App\Http\Controllers\Api\V1\DeliveryController::class, 'livreurs'])->name('deliveries.livreurs');

            // Emplettes - côté admin
            Route::get('/admin/emplettes', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'adminIndex'])->name('admin.emplettes.index');
            Route::post('/admin/emplettes/{emplette}/quote', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'adminUpdate'])->name('admin.emplettes.quote');
            Route::post('/admin/emplettes/{emplette}/convert', [\App\Http\Controllers\Api\V1\EmpletteController::class, 'convertToOrder'])->name('admin.emplettes.convert');
        });

        // Admin Banners
        Route::middleware('role:super_admin,admin')->group(function () {
            Route::get('/admin/banners', [\App\Http\Controllers\Api\V1\BannerController::class, 'adminIndex'])->name('admin.banners.index');
            Route::get('/admin/banners/{banner}', [\App\Http\Controllers\Api\V1\BannerController::class, 'adminShow'])->name('admin.banners.show');
            Route::post('/admin/banners', [\App\Http\Controllers\Api\V1\BannerController::class, 'adminStore'])->name('admin.banners.store');
            Route::put('/admin/banners/{banner}', [\App\Http\Controllers\Api\V1\BannerController::class, 'adminUpdate'])->name('admin.banners.update');
            Route::delete('/admin/banners/{banner}', [\App\Http\Controllers\Api\V1\BannerController::class, 'adminDestroy'])->name('admin.banners.destroy');
        });

        Route::middleware('role:livreur')->group(function () {
            Route::get('/deliveries/my', [\App\Http\Controllers\Api\V1\DeliveryController::class, 'my'])->name('deliveries.my');
            Route::post('/deliveries/{order}/status', [\App\Http\Controllers\Api\V1\DeliveryController::class, 'updateStatus'])->name('deliveries.status');
        });
    });
});
