<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Admin\AdminLoginController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\SubCategoryController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\BannerController;
use App\Http\Controllers\Admin\PaymentMethodController;
use App\Http\Controllers\Admin\DeliveryTypeController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\Admin\Ai\SettingsController as AdminAiSettingsController;
use App\Http\Controllers\Admin\NewsletterSubscriptionController as AdminNewsletterSubscriptionController;
use App\Http\Controllers\Admin\SupportTicketController as AdminSupportTicketController;
use App\Http\Controllers\Admin\StaffController;

Route::get('/media/{path}', function (string $path) {
    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    return Storage::disk('public')->response($path);
})->where('path', '.*')->name('public.media');

Route::get('/', function () {
    if (auth('admin')->check()) {
        return redirect()->route('admin.dashboard');
    }

    return redirect()->route('admin.login');
})->name('home');

Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/', function () {
        return redirect()->route('admin.dashboard');
    });
    Route::get('/login', [AdminLoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [AdminLoginController::class, 'login'])->name('login.submit');
    Route::post('/logout', [AdminLoginController::class, 'logout'])->name('logout');

    Route::middleware(['auth:admin', 'demo_readonly'])->group(function () {
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
        Route::resource('categories', CategoryController::class);
        Route::resource('subcategories', SubCategoryController::class);
        Route::match(['put', 'patch'], 'subcategories', [SubCategoryController::class, 'upsert'])->name('subcategories.upsert');
        Route::get('subcategories/by-category/{category}', [SubCategoryController::class, 'getByCategory'])->name('subcategories.by-category');
        Route::resource('products', ProductController::class);
        Route::get('premium-products', [ProductController::class, 'premium'])->name('products.premium');
        Route::get('premium-products/select', [ProductController::class, 'premiumSelect'])->name('products.premium.select');
        Route::get('premium-products/available', [ProductController::class, 'premiumAvailable'])->name('products.premium.available');
        Route::post('premium-products/attach', [ProductController::class, 'premiumAttach'])->name('products.premium.attach');
        Route::post('premium-products/detach', [ProductController::class, 'premiumDetach'])->name('products.premium.detach');
        Route::get('flash-sale', [ProductController::class, 'flashSale'])->name('products.flash_sale');
        Route::post('flash-sale/schedule', [ProductController::class, 'updateFlashSaleSchedule'])->name('products.flash_sale.schedule');
        Route::get('flash-sale/select', [ProductController::class, 'flashSaleSelect'])->name('products.flash_sale.select');
        Route::get('flash-sale/available', [ProductController::class, 'flashSaleAvailable'])->name('products.flash_sale.available');
        Route::post('flash-sale/attach', [ProductController::class, 'flashSaleAttach'])->name('products.flash_sale.attach');
        Route::post('flash-sale/detach', [ProductController::class, 'flashSaleDetach'])->name('products.flash_sale.detach');
        Route::get('special-offers', [ProductController::class, 'special'])->name('products.special');
        Route::get('special-offers/select', [ProductController::class, 'specialSelect'])->name('products.special.select');
        Route::get('special-offers/available', [ProductController::class, 'specialAvailable'])->name('products.special.available');
        Route::post('special-offers/attach', [ProductController::class, 'specialAttach'])->name('products.special.attach');
        Route::post('special-offers/detach', [ProductController::class, 'specialDetach'])->name('products.special.detach');
        Route::resource('banners', BannerController::class);
        Route::resource('payment-methods', PaymentMethodController::class);
        Route::resource('delivery-types', DeliveryTypeController::class);
        Route::resource('coupons', \App\Http\Controllers\Admin\CouponController::class);
        Route::resource('pages', \App\Http\Controllers\Admin\PageController::class);
        
        // Orders
        Route::resource('orders', \App\Http\Controllers\Admin\OrderController::class)->only(['index', 'show']);
        Route::get('orders/export/csv', [\App\Http\Controllers\Admin\OrderController::class, 'export'])->name('orders.export');
        Route::patch('orders/{order}/status', [\App\Http\Controllers\Admin\OrderController::class, 'updateStatus'])->name('orders.update_status');

        // Customers
        Route::resource('customers', \App\Http\Controllers\Admin\CustomerController::class)->only(['index', 'show']);

        // Support Tickets
        Route::get('support-tickets', [AdminSupportTicketController::class, 'index'])->name('support_tickets.index');
        Route::get('support-tickets/{id}', [AdminSupportTicketController::class, 'show'])->name('support_tickets.show');
        Route::post('support-tickets/{id}/reply', [AdminSupportTicketController::class, 'reply'])->name('support_tickets.reply');
        Route::post('support-tickets/{id}/status', [AdminSupportTicketController::class, 'updateStatus'])->name('support_tickets.update_status');

        Route::get('/newsletter-subscriptions', [AdminNewsletterSubscriptionController::class, 'index'])->name('newsletter-subscriptions.index');

        // Settings
        Route::get('/settings', [\App\Http\Controllers\Admin\SettingsController::class, 'index'])->name('settings.index');
        Route::post('/settings', [\App\Http\Controllers\Admin\SettingsController::class, 'update'])->name('settings.update');

        // AI Assistant Settings
        Route::get('/ai/settings', [AdminAiSettingsController::class, 'index'])->name('ai.settings.index');
        Route::post('/ai/settings', [AdminAiSettingsController::class, 'update'])->name('ai.settings.update');

        // Reviews Selection for Homepage
        Route::get('/reviews/select', [\App\Http\Controllers\Admin\ReviewSelectionController::class, 'index'])->name('reviews.select');
        Route::post('/reviews/select', [\App\Http\Controllers\Admin\ReviewSelectionController::class, 'save'])->name('reviews.select.save');
        Route::post('/reviews/select/remove', [\App\Http\Controllers\Admin\ReviewSelectionController::class, 'remove'])->name('reviews.select.remove');

        // Push Notifications (OneSignal)
        Route::get('/push-notifications', [\App\Http\Controllers\Admin\PushNotificationController::class, 'index'])->name('notifications.index');
        Route::get('/push-notifications/create', [\App\Http\Controllers\Admin\PushNotificationController::class, 'create'])->name('notifications.create');
        Route::post('/push-notifications', [\App\Http\Controllers\Admin\PushNotificationController::class, 'store'])->name('notifications.store');

        // Special Offers Banners (page-specific listing)
        Route::prefix('special-offers/banners')->name('special.banners.')->group(function () {
            Route::get('/', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'index'])->name('index');
            Route::get('/create', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'create'])->name('create');
            Route::post('/', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'store'])->name('store');
            Route::get('/{banner}/edit', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'edit'])->name('edit');
            Route::put('/{banner}', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'update'])->name('update');
            Route::delete('/{banner}', [\App\Http\Controllers\Admin\SpecialOfferBannerController::class, 'destroy'])->name('destroy');
        });

        Route::middleware('super_admin')->group(function () {
            Route::get('staff', [StaffController::class, 'index'])->name('staff.index');
            Route::get('staff/create', [StaffController::class, 'create'])->name('staff.create');
            Route::post('staff', [StaffController::class, 'store'])->name('staff.store');
            Route::get('staff/{staff}/edit', [StaffController::class, 'edit'])->name('staff.edit');
            Route::put('staff/{staff}', [StaffController::class, 'update'])->name('staff.update');
            Route::delete('staff/{staff}', [StaffController::class, 'destroy'])->name('staff.destroy');
            Route::post('staff/{staff}/toggle-status', [StaffController::class, 'toggleStatus'])->name('staff.toggle_status');
        });
    });
});

Route::fallback(function () {
    return redirect()->route('home');
});
