<?php

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

Route::middleware('not.installed')->prefix('install')->group(function () {
    Route::get('/', [InstallController::class, 'index'])->name('install.index');
    Route::get('/requirements', [InstallController::class, 'requirements'])->name('install.requirements');
    Route::get('/database', [InstallController::class, 'databaseForm'])->name('install.database');
    Route::post('/database', [InstallController::class, 'databaseStore'])->name('install.database.store');
    Route::get('/admin', [InstallController::class, 'adminForm'])->name('install.admin');
    Route::post('/admin', [InstallController::class, 'adminStore'])->name('install.admin.store');
    Route::get('/complete', [InstallController::class, 'complete'])->name('install.complete');
});

Route::middleware('installed')->group(function () {
    Route::get('/admin/login', [\App\Http\Controllers\Auth\LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/admin/login', [\App\Http\Controllers\Auth\LoginController::class, 'login']);
    Route::post('/admin/logout', [\App\Http\Controllers\Auth\LoginController::class, 'logout'])->name('logout');
    Route::get('/branding/logo', function () {
        return response()->file(base_path('icon.png'));
    })->name('branding.logo');

    Route::get('/favicon-site.png', \App\Http\Controllers\SiteFaviconController::class)->name('site.favicon');

    Route::get('/', [\App\Http\Controllers\FrontController::class, 'home'])->name('home');
    Route::get('/locale/{locale}', [\App\Http\Controllers\LocaleController::class, 'switch'])->name('locale.switch')->where('locale', '[a-z]{2}');
    Route::post('/contact', [\App\Http\Controllers\ContactController::class, 'store'])->name('contact.store');
    Route::middleware('plugin.active:newsletter')->post('/newsletter/subscribe', [\App\Http\Controllers\NewsletterController::class, 'subscribe'])->name('newsletter.subscribe');

    Route::middleware('plugin.active:forum')->prefix('forum')->name('forum.')->group(function () {
        Route::get('/', [\App\Http\Controllers\ForumController::class, 'index'])->name('index');
        Route::get('/{slug}/nouveau', [\App\Http\Controllers\ForumController::class, 'createThread'])->name('create-thread');
        Route::post('/{slug}', [\App\Http\Controllers\ForumController::class, 'storeThread'])->middleware('throttle:10,1')->name('store-thread');
        Route::get('/{categorySlug}/{threadSlug}', [\App\Http\Controllers\ForumController::class, 'thread'])->name('thread');
        Route::post('/{categorySlug}/{threadSlug}', [\App\Http\Controllers\ForumController::class, 'storeReply'])->middleware('throttle:20,1')->name('reply');
        Route::get('/{slug}', [\App\Http\Controllers\ForumController::class, 'category'])->name('category');
    });

    Route::middleware('plugin.active:ecommerce')->prefix('boutique')->name('ecommerce.')->group(function () {
        Route::get('/', [\App\Http\Controllers\EcommerceController::class, 'index'])->name('index');
        Route::get('/categorie/{slug}', [\App\Http\Controllers\EcommerceController::class, 'category'])->name('category');
        Route::get('/produit/{slug}', [\App\Http\Controllers\EcommerceController::class, 'product'])->name('product');
        Route::get('/panier', [\App\Http\Controllers\EcommerceController::class, 'cart'])->name('cart');
        Route::post('/panier/ajouter', [\App\Http\Controllers\EcommerceController::class, 'addToCart'])->name('add-to-cart');
        Route::post('/panier/modifier', [\App\Http\Controllers\EcommerceController::class, 'updateCart'])->name('update-cart');
        Route::post('/panier/code-promo', [\App\Http\Controllers\EcommerceController::class, 'applyCoupon'])->name('apply-coupon');
        Route::post('/panier/code-promo/supprimer', [\App\Http\Controllers\EcommerceController::class, 'removeCoupon'])->name('remove-coupon');
        Route::get('/commander', [\App\Http\Controllers\EcommerceController::class, 'checkout'])->name('checkout');
        Route::post('/commander', [\App\Http\Controllers\EcommerceController::class, 'placeOrder'])->name('place-order');
        Route::get('/paiement/{order}', [\App\Http\Controllers\EcommerceController::class, 'payment'])->name('payment');
        Route::post('/commander/create-payment-intent', [\App\Http\Controllers\EcommerceController::class, 'createPaymentIntent'])->name('create-payment-intent');
        Route::get('/commande/{order}', [\App\Http\Controllers\EcommerceController::class, 'orderConfirmation'])->name('order-confirmation');
    });

    Route::post('/webhooks/stripe', [\App\Http\Controllers\StripeWebhookController::class, 'handle'])->name('stripe.webhook')->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);
    Route::post('/webhooks/stripe-marketplace', [\App\Http\Controllers\MarketplaceStripeWebhookController::class, 'handle'])->name('stripe.marketplace.webhook')->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);
    Route::middleware('plugin.active:ecommerce')->post('/boutique/stripe-webhook', [\App\Http\Controllers\StripeWebhookController::class, 'handle'])->name('ecommerce.stripe-webhook')->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);

    Route::middleware('plugin.active:reservation')->group(function () {
        $prefix = config('reservation.public_prefix', 'reservation');
        if (! in_array($prefix, ['reservation', 'events'], true)) {
            $prefix = 'reservation';
        }
        $altPrefix = $prefix === 'reservation' ? 'events' : 'reservation';

        Route::prefix($prefix)->name('reservation.')->group(function () {
            Route::get('/', [\App\Http\Controllers\ReservationController::class, 'index'])->name('index');
            Route::get('/confirmation/{token}', [\App\Http\Controllers\ReservationController::class, 'confirmation'])->name('confirmation');
            Route::get('/dates-disponibles', [\App\Http\Controllers\ReservationController::class, 'availableDatesAll'])->name('available-dates-all');
            Route::get('/{service}/dates-disponibles', [\App\Http\Controllers\ReservationController::class, 'availableDates'])->name('available-dates');
            Route::get('/{service}/creneaux', [\App\Http\Controllers\ReservationController::class, 'slots'])->name('slots');
            Route::get('/{service}', [\App\Http\Controllers\ReservationController::class, 'book'])->name('book');
            Route::post('/', [\App\Http\Controllers\ReservationController::class, 'store'])->name('store');
        });

        Route::redirect($altPrefix, '/'.$prefix, 301);
        Route::get($altPrefix.'/{path}', function (string $path) use ($prefix) {
            $target = '/'.trim($prefix, '/').'/'.ltrim($path, '/');
            $qs = request()->getQueryString();
            if ($qs !== null && $qs !== '') {
                $target .= '?'.$qs;
            }

            return redirect($target, 301);
        })->where('path', '.*');
    });

    Route::get('/{slug}', [\App\Http\Controllers\FrontController::class, 'page'])->name('page')->where('slug', '(?!admin$|forum|boutique|reservation|events)[a-z0-9\-]+');

    Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin', 'admin.can_edit', 'newsletter.scheduler'])->group(function () {
        Route::get('/', [\App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('dashboard');

        Route::middleware('admin.only')->group(function () {
            Route::get('users', [\App\Http\Controllers\Admin\UserController::class, 'index'])->name('users.index');
            Route::get('users/create', [\App\Http\Controllers\Admin\UserController::class, 'create'])->name('users.create');
            Route::post('users', [\App\Http\Controllers\Admin\UserController::class, 'store'])->name('users.store');
            Route::get('users/{user}/edit', [\App\Http\Controllers\Admin\UserController::class, 'edit'])->name('users.edit');
            Route::match(['put', 'patch'], 'users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'update'])->name('users.update');
            Route::delete('users/{user}', [\App\Http\Controllers\Admin\UserController::class, 'destroy'])->name('users.destroy');
        });

        Route::get('pages/home/edit', [\App\Http\Controllers\Admin\PageController::class, 'editHome'])->name('pages.edit-home');
        Route::resource('pages', \App\Http\Controllers\Admin\PageController::class);
        Route::resource('media', \App\Http\Controllers\Admin\MediaController::class)->only(['index', 'store', 'destroy']);
        Route::post('media/upload', [\App\Http\Controllers\Admin\MediaController::class, 'uploadApi'])->name('media.upload');
        Route::get('appearance', [\App\Http\Controllers\Admin\AppearanceController::class, 'index'])->name('appearance.index');
        Route::post('appearance', [\App\Http\Controllers\Admin\AppearanceController::class, 'store'])->name('appearance.store');
        Route::middleware('admin.only')->group(function () {
            Route::get('settings/email', [\App\Http\Controllers\Admin\SettingsController::class, 'email'])->name('settings.email');
            Route::post('settings/email', [\App\Http\Controllers\Admin\SettingsController::class, 'storeEmail'])->name('settings.email.store');
            Route::post('settings/email/test', [\App\Http\Controllers\Admin\SettingsController::class, 'sendTestEmail'])->name('settings.email.test');
            Route::get('settings/legal-popup', [\App\Http\Controllers\Admin\SettingsController::class, 'legalPopup'])->name('settings.legal-popup');
            Route::post('settings/legal-popup', [\App\Http\Controllers\Admin\SettingsController::class, 'storeLegalPopup'])->name('settings.legal-popup.store');
            Route::get('settings/analytics', [\App\Http\Controllers\Admin\SettingsController::class, 'analytics'])->name('settings.analytics');
            Route::post('settings/analytics', [\App\Http\Controllers\Admin\SettingsController::class, 'storeAnalytics'])->name('settings.analytics.store');
            Route::get('settings/gcms-update', [\App\Http\Controllers\Admin\GcmsUpdateController::class, 'index'])->name('settings.gcms-update');
            Route::post('settings/gcms-update/check', [\App\Http\Controllers\Admin\GcmsUpdateController::class, 'check'])->name('settings.gcms-update.check');
            Route::post('settings/gcms-update/apply', [\App\Http\Controllers\Admin\GcmsUpdateController::class, 'apply'])->name('settings.gcms-update.apply');
            Route::get('marketplace', [\App\Http\Controllers\Admin\MarketplaceController::class, 'index'])->name('marketplace.index');
            Route::get('marketplace/themes', [\App\Http\Controllers\Admin\MarketplaceController::class, 'themes'])->name('marketplace.themes');
            Route::get('marketplace/plugins', [\App\Http\Controllers\Admin\MarketplaceController::class, 'plugins'])->name('marketplace.plugins');
            Route::get('marketplace/success', [\App\Http\Controllers\Admin\MarketplaceController::class, 'success'])->name('marketplace.success');
            Route::get('marketplace/settings', [\App\Http\Controllers\Admin\MarketplaceController::class, 'settings'])->name('marketplace.settings');
            Route::post('marketplace/purchase', [\App\Http\Controllers\Admin\MarketplaceController::class, 'purchase'])->name('marketplace.purchase');
            Route::post('marketplace/activate', [\App\Http\Controllers\Admin\MarketplaceController::class, 'activate'])->name('marketplace.activate');
            Route::post('marketplace/upload-theme', [\App\Http\Controllers\Admin\ContentUploadController::class, 'uploadTheme'])->name('marketplace.upload-theme');
            Route::post('marketplace/upload-plugin', [\App\Http\Controllers\Admin\ContentUploadController::class, 'uploadPlugin'])->name('marketplace.upload-plugin');
        });
        Route::resource('contact-messages', \App\Http\Controllers\Admin\ContactMessageController::class)->only(['index', 'show', 'destroy'])->names('contact-messages');

        Route::middleware('plugin.active:forum')->prefix('forum')->name('forum.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\ForumController::class, 'index'])->name('index');
            Route::get('/categories', [\App\Http\Controllers\Admin\ForumController::class, 'categories'])->name('categories');
            Route::post('/categories', [\App\Http\Controllers\Admin\ForumController::class, 'storeCategory'])->name('categories.store');
            Route::match(['put', 'patch'], '/categories/{forumCategory}', [\App\Http\Controllers\Admin\ForumController::class, 'updateCategory'])->name('categories.update');
            Route::delete('/categories/{forumCategory}', [\App\Http\Controllers\Admin\ForumController::class, 'destroyCategory'])->name('categories.destroy');
            Route::get('/threads', [\App\Http\Controllers\Admin\ForumController::class, 'threads'])->name('threads');
            Route::get('/threads/{forumThread}', [\App\Http\Controllers\Admin\ForumController::class, 'thread'])->name('thread');
            Route::match(['put', 'patch'], '/threads/{forumThread}', [\App\Http\Controllers\Admin\ForumController::class, 'updateThread'])->name('thread.update');
            Route::delete('/threads/{forumThread}', [\App\Http\Controllers\Admin\ForumController::class, 'destroyThread'])->name('thread.destroy');
            Route::post('/threads/{forumThread}/approve', [\App\Http\Controllers\Admin\ForumController::class, 'approveThread'])->name('thread.approve');
            Route::delete('/replies/{forumReply}', [\App\Http\Controllers\Admin\ForumController::class, 'destroyReply'])->name('reply.destroy');
            Route::post('/replies/{forumReply}/approve', [\App\Http\Controllers\Admin\ForumController::class, 'approveReply'])->name('reply.approve');
        });

        Route::middleware('plugin.active:ecommerce')->prefix('ecommerce')->name('ecommerce.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\EcommerceController::class, 'index'])->name('index');
            Route::get('/settings', [\App\Http\Controllers\Admin\EcommerceController::class, 'settings'])->name('settings');
            Route::post('/settings', [\App\Http\Controllers\Admin\EcommerceController::class, 'storeSettings'])->name('settings.store');
            Route::get('/categories', [\App\Http\Controllers\Admin\EcommerceController::class, 'categories'])->name('categories');
            Route::post('/categories', [\App\Http\Controllers\Admin\EcommerceController::class, 'storeCategory'])->name('categories.store');
            Route::match(['put', 'patch'], '/categories/{productCategory}', [\App\Http\Controllers\Admin\EcommerceController::class, 'updateCategory'])->name('categories.update');
            Route::delete('/categories/{productCategory}', [\App\Http\Controllers\Admin\EcommerceController::class, 'destroyCategory'])->name('categories.destroy');
            Route::get('/shipping-methods', [\App\Http\Controllers\Admin\EcommerceController::class, 'shippingMethods'])->name('shipping-methods');
            Route::get('/shipping-methods/create', [\App\Http\Controllers\Admin\EcommerceController::class, 'createShippingMethod'])->name('shipping-methods.create');
            Route::post('/shipping-methods', [\App\Http\Controllers\Admin\EcommerceController::class, 'storeShippingMethod'])->name('shipping-methods.store');
            Route::get('/shipping-methods/{shippingMethod}/edit', [\App\Http\Controllers\Admin\EcommerceController::class, 'editShippingMethod'])->name('shipping-methods.edit');
            Route::match(['put', 'patch'], '/shipping-methods/{shippingMethod}', [\App\Http\Controllers\Admin\EcommerceController::class, 'updateShippingMethod'])->name('shipping-methods.update');
            Route::delete('/shipping-methods/{shippingMethod}', [\App\Http\Controllers\Admin\EcommerceController::class, 'destroyShippingMethod'])->name('shipping-methods.destroy');
            Route::get('/products', [\App\Http\Controllers\Admin\EcommerceController::class, 'products'])->name('products');
            Route::get('/products/create', [\App\Http\Controllers\Admin\EcommerceController::class, 'createProduct'])->name('products.create');
            Route::post('/products', [\App\Http\Controllers\Admin\EcommerceController::class, 'storeProduct'])->name('products.store');
            Route::get('/products/{product}/edit', [\App\Http\Controllers\Admin\EcommerceController::class, 'editProduct'])->name('products.edit');
            Route::match(['put', 'patch'], '/products/{product}', [\App\Http\Controllers\Admin\EcommerceController::class, 'updateProduct'])->name('products.update');
            Route::delete('/products/{product}', [\App\Http\Controllers\Admin\EcommerceController::class, 'destroyProduct'])->name('products.destroy');
            Route::get('/orders', [\App\Http\Controllers\Admin\EcommerceController::class, 'orders'])->name('orders');
            Route::get('/orders/{order}', [\App\Http\Controllers\Admin\EcommerceController::class, 'showOrder'])->name('orders.show');
            Route::post('/orders/{order}/status', [\App\Http\Controllers\Admin\EcommerceController::class, 'updateOrderStatus'])->name('orders.status');
            Route::get('/coupons', [\App\Http\Controllers\Admin\EcommerceController::class, 'coupons'])->name('coupons');
            Route::get('/coupons/create', [\App\Http\Controllers\Admin\EcommerceController::class, 'createCoupon'])->name('coupons.create');
            Route::post('/coupons', [\App\Http\Controllers\Admin\EcommerceController::class, 'storeCoupon'])->name('coupons.store');
            Route::get('/coupons/{coupon}/edit', [\App\Http\Controllers\Admin\EcommerceController::class, 'editCoupon'])->name('coupons.edit');
            Route::match(['put', 'patch'], '/coupons/{coupon}', [\App\Http\Controllers\Admin\EcommerceController::class, 'updateCoupon'])->name('coupons.update');
            Route::delete('/coupons/{coupon}', [\App\Http\Controllers\Admin\EcommerceController::class, 'destroyCoupon'])->name('coupons.destroy');
        });

        Route::middleware('plugin.active:newsletter')->prefix('newsletter')->name('newsletter.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\NewsletterController::class, 'index'])->name('index');
            Route::get('/settings', [\App\Http\Controllers\Admin\NewsletterController::class, 'settings'])->name('settings');
            Route::post('/settings', [\App\Http\Controllers\Admin\NewsletterController::class, 'settingsStore'])->name('settings.store');
            Route::get('/campaigns', [\App\Http\Controllers\Admin\NewsletterController::class, 'campaigns'])->name('campaigns');
            Route::get('/campaigns/create', [\App\Http\Controllers\Admin\NewsletterController::class, 'createCampaign'])->name('campaigns.create');
            Route::post('/campaigns', [\App\Http\Controllers\Admin\NewsletterController::class, 'storeCampaign'])->name('campaigns.store');
            Route::get('/campaigns/{newsletter}/edit', [\App\Http\Controllers\Admin\NewsletterController::class, 'editCampaign'])->name('campaigns.edit');
            Route::match(['put', 'patch'], '/campaigns/{newsletter}', [\App\Http\Controllers\Admin\NewsletterController::class, 'updateCampaign'])->name('campaigns.update');
            Route::post('/campaigns/{newsletter}/send', [\App\Http\Controllers\Admin\NewsletterController::class, 'sendCampaign'])->name('campaigns.send');
            Route::get('/export', [\App\Http\Controllers\Admin\NewsletterController::class, 'exportCsv'])->name('export');
            Route::delete('/{newsletterSubscriber}', [\App\Http\Controllers\Admin\NewsletterController::class, 'destroy'])->name('destroy');
        });

        Route::middleware('plugin.active:multilang')->prefix('multilang')->name('multilang.')->group(function () {
            Route::get('/settings', [\App\Http\Controllers\Admin\MultilangController::class, 'settings'])->name('settings');
            Route::post('/settings', [\App\Http\Controllers\Admin\MultilangController::class, 'settingsStore'])->name('settings.store');
            Route::post('/translate-all', [\App\Http\Controllers\Admin\MultilangController::class, 'translateAll'])->name('translate-all');
        });

        Route::middleware('plugin.active:captcha-protection')->prefix('captcha-protection')->name('captcha-protection.')->group(function () {
            Route::get('/settings', [\App\Http\Controllers\Admin\CaptchaProtectionController::class, 'settings'])->name('settings');
            Route::post('/settings', [\App\Http\Controllers\Admin\CaptchaProtectionController::class, 'store'])->name('settings.store');
        });

        Route::middleware('plugin.active:banners')->prefix('banners')->name('banners.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\BannerController::class, 'index'])->name('index');
            Route::get('/create', [\App\Http\Controllers\Admin\BannerController::class, 'create'])->name('create');
            Route::post('/', [\App\Http\Controllers\Admin\BannerController::class, 'store'])->name('store');
            Route::get('/{banner}/edit', [\App\Http\Controllers\Admin\BannerController::class, 'edit'])->name('edit');
            Route::match(['put', 'patch'], '/{banner}', [\App\Http\Controllers\Admin\BannerController::class, 'update'])->name('update');
            Route::delete('/{banner}', [\App\Http\Controllers\Admin\BannerController::class, 'destroy'])->name('destroy');
        });

        Route::middleware('plugin.active:reservation')->prefix('reservation')->name('reservation.')->group(function () {
            Route::post('/public-url', [\App\Http\Controllers\Admin\ReservationController::class, 'updatePublicUrl'])->name('public-url');
            Route::get('/', [\App\Http\Controllers\Admin\ReservationController::class, 'index'])->name('index');
            Route::get('/services', [\App\Http\Controllers\Admin\ReservationController::class, 'services'])->name('services');
            Route::get('/services/create', [\App\Http\Controllers\Admin\ReservationController::class, 'createService'])->name('services.create');
            Route::get('/services/{reservationService}/edit', [\App\Http\Controllers\Admin\ReservationController::class, 'editService'])->name('services.edit');
            Route::post('/services', [\App\Http\Controllers\Admin\ReservationController::class, 'storeService'])->name('services.store');
            Route::match(['put', 'patch'], '/services/{reservationService}', [\App\Http\Controllers\Admin\ReservationController::class, 'updateService'])->name('services.update');
            Route::delete('/services/{reservationService}', [\App\Http\Controllers\Admin\ReservationController::class, 'destroyService'])->name('services.destroy');
            Route::get('/slots', [\App\Http\Controllers\Admin\ReservationController::class, 'slots'])->name('slots');
            Route::post('/slots', [\App\Http\Controllers\Admin\ReservationController::class, 'storeSlots'])->name('slots.store');
            Route::get('/reservations', [\App\Http\Controllers\Admin\ReservationController::class, 'reservations'])->name('reservations');
            Route::get('/reservations/{reservation}', [\App\Http\Controllers\Admin\ReservationController::class, 'showReservation'])->name('reservations.show');
            Route::post('/reservations/{reservation}/status', [\App\Http\Controllers\Admin\ReservationController::class, 'updateReservationStatus'])->name('reservations.status');
            Route::delete('/reservations/{reservation}', [\App\Http\Controllers\Admin\ReservationController::class, 'destroyReservation'])->name('reservations.destroy');
        });
    });
});
