<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Seller\SellerController;
use App\Http\Controllers\Buyer\BuyerController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\ProductController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/



// Authentication Routes
Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('login', [LoginController::class, 'login']);
Route::post('logout', [LoginController::class, 'logout'])->name('logout');

// Public Help Page
Route::get('help', [\App\Http\Controllers\Public\HelpController::class, 'index'])->name('public.help');

// Static Pages (About, Contact, Terms, Privacy)
Route::get('about', [\App\Http\Controllers\Public\PageController::class, 'about'])->name('public.about');
Route::get('contact', [\App\Http\Controllers\Public\PageController::class, 'contact'])->name('public.contact');
Route::post('contact', [\App\Http\Controllers\Public\PageController::class, 'submitContact'])->name('public.contact.submit');
Route::get('terms', [\App\Http\Controllers\Public\PageController::class, 'terms'])->name('public.terms');
Route::get('privacy', [\App\Http\Controllers\Public\PageController::class, 'privacy'])->name('public.privacy');

// API Routes for AJAX calls
Route::get('api/categories/{category}/children', function(\App\Models\Category $category) {
    return $category->children()->select('id', 'name')->get();
});

// Social Login Routes
Route::get('auth/google', [\App\Http\Controllers\Auth\SocialAuthController::class, 'redirectToGoogle'])->name('auth.google');
Route::get('auth/google/callback', [\App\Http\Controllers\Auth\SocialAuthController::class, 'handleGoogleCallback']);
Route::get('auth/facebook', [\App\Http\Controllers\Auth\SocialAuthController::class, 'redirectToFacebook'])->name('auth.facebook');
Route::get('auth/facebook/callback', [\App\Http\Controllers\Auth\SocialAuthController::class, 'handleFacebookCallback']);

// Impersonation Leave
Route::middleware(['auth'])->get('impersonate/leave', [AdminController::class, 'stopImpersonating'])->name('impersonate.leave');

// Notifications (Authenticated Users)
Route::middleware(['auth'])->prefix('notifications')->name('notifications.')->group(function () {
    Route::get('/', [\App\Http\Controllers\NotificationController::class, 'index'])->name('index');
    Route::get('/unread-count', [\App\Http\Controllers\NotificationController::class, 'unreadCount'])->name('unread-count');
    Route::get('/all', [\App\Http\Controllers\NotificationController::class, 'all'])->name('all');
    Route::post('/{notification}/read', [\App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('read');
    Route::post('/mark-all-read', [\App\Http\Controllers\NotificationController::class, 'markAllAsRead'])->name('mark-all-read');
    Route::delete('/{notification}', [\App\Http\Controllers\NotificationController::class, 'destroy'])->name('destroy');
});

// Password Reset Routes
Route::get('password/reset', [\App\Http\Controllers\Auth\ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
Route::post('password/email', [\App\Http\Controllers\Auth\ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');
Route::get('password/reset/{token}', [\App\Http\Controllers\Auth\ResetPasswordController::class, 'showResetForm'])->name('password.reset');
Route::post('password/reset', [\App\Http\Controllers\Auth\ResetPasswordController::class, 'reset'])->name('password.update');

Route::get('register/seller', [RegisterController::class, 'showSellerRegistrationForm'])->name('register.seller');
Route::post('register/seller', [RegisterController::class, 'registerSeller']);

Route::get('register/buyer', [RegisterController::class, 'showBuyerRegistrationForm'])->name('register.buyer');
Route::post('register/buyer', [RegisterController::class, 'registerBuyer']);

// Protected Routes
Route::middleware(['auth', 'role:seller'])->prefix('seller')->group(function () {
    Route::get('dashboard', [SellerController::class, 'dashboard'])->name('seller.dashboard');
    Route::get('profile', [SellerController::class, 'profile'])->name('seller.profile');
    Route::post('profile', [SellerController::class, 'updateProfile'])->name('seller.profile.update');
    Route::delete('profile', [SellerController::class, 'destroy'])->name('seller.profile.destroy');
    Route::post('profile/delete/request', [SellerController::class, 'requestAccountDeletion'])->name('seller.profile.delete.request');
    Route::post('profile/delete/cancel', [SellerController::class, 'cancelAccountDeletion'])->name('seller.profile.delete.cancel');
    
    // Verification
    Route::get('verification', [SellerController::class, 'verification'])->name('seller.verification.index');
    Route::post('verification/upload', [SellerController::class, 'uploadKyc'])->name('seller.verification.upload');
    Route::post('kyc/update', [SellerController::class, 'updateKycFromProfile'])->name('seller.kyc.update');
    Route::delete('kyc/document/{document}', [SellerController::class, 'deleteKycDocument'])->name('seller.kyc.document.destroy');
    
    // Product Management
    Route::post('products/{product}/update-fix', [\App\Http\Controllers\Seller\ProductController::class, 'update'])->name('seller.products.update_fix');
    Route::resource('products', \App\Http\Controllers\Seller\ProductController::class, ['as' => 'seller']);
    
    // Bulk Upload
    Route::get('products/bulk-upload', [\App\Http\Controllers\Seller\BulkUploadController::class, 'index'])->name('seller.products.bulk-upload');
    Route::get('products/bulk-upload/template', [\App\Http\Controllers\Seller\BulkUploadController::class, 'downloadTemplate'])->name('seller.products.bulk-upload.template');
    Route::post('products/bulk-upload/upload', [\App\Http\Controllers\Seller\BulkUploadController::class, 'upload'])->name('seller.products.bulk-upload.upload');
    
    Route::get('rfqs', [SellerController::class, 'rfqs'])->name('seller.rfqs');
    // Submit Quote Route
    Route::post('rfqs/{rfq}/quote', [SellerController::class, 'submitQuote'])->name('seller.rfqs.quote');
    Route::get('inquiries', [SellerController::class, 'inquiries'])->name('seller.inquiries.index');
    Route::post('inquiries/{inquiry}/reply', [SellerController::class, 'replyToInquiry'])->name('seller.inquiries.reply');
    Route::get('transactions', [SellerController::class, 'transactions'])->name('seller.transactions.index');
    Route::get('subscription', [\App\Http\Controllers\Seller\SubscriptionController::class, 'index'])->name('seller.subscription.index');
    Route::get('subscription/checkout/{plan}', [\App\Http\Controllers\Seller\SubscriptionController::class, 'checkout'])->name('seller.subscription.checkout');
    Route::post('subscription/success', [\App\Http\Controllers\Seller\SubscriptionController::class, 'paymentSuccess'])->name('seller.subscription.success');
    Route::get('subscription/paypal/success', [\App\Http\Controllers\Seller\SubscriptionController::class, 'paypalSuccess'])->name('seller.subscription.paypal.success');
    Route::post('subscription/paypal/initiate', [\App\Http\Controllers\Seller\SubscriptionController::class, 'initiatePaypal'])->name('seller.subscription.paypal.initiate');
    Route::post('subscription/mock', [\App\Http\Controllers\Seller\SubscriptionController::class, 'mockPayment'])->name('seller.subscription.mock');

    // Microsite Routes
    Route::get('microsite', [\App\Http\Controllers\Seller\MicrositeController::class, 'index'])->name('seller.microsite.index');
    Route::post('microsite/banner', [\App\Http\Controllers\Seller\MicrositeController::class, 'storeBanner'])->name('seller.microsite.banner.store');
    Route::delete('microsite/banner/{banner}', [\App\Http\Controllers\Seller\MicrositeController::class, 'destroyBanner'])->name('seller.microsite.banner.destroy');
    Route::post('microsite/certificate', [\App\Http\Controllers\Seller\MicrositeController::class, 'storeCertificate'])->name('seller.microsite.certificate.store');
    Route::delete('microsite/certificate/{certificate}', [\App\Http\Controllers\Seller\MicrositeController::class, 'destroyCertificate'])->name('seller.microsite.certificate.destroy');
    Route::post('microsite/page', [\App\Http\Controllers\Seller\MicrositeController::class, 'storePage'])->name('seller.microsite.page.store');
    Route::put('microsite/page/{page}', [\App\Http\Controllers\Seller\MicrositeController::class, 'updatePage'])->name('seller.microsite.page.update');
    Route::delete('microsite/page/{page}', [\App\Http\Controllers\Seller\MicrositeController::class, 'destroyPage'])->name('seller.microsite.page.destroy');
    Route::post('microsite/settings', [\App\Http\Controllers\Seller\MicrositeController::class, 'updateSettings'])->name('seller.microsite.settings.update');
    Route::get('analytics', [SellerController::class, 'analytics'])->name('seller.analytics');
    
    // KYC Verification
    Route::get('verification', [SellerController::class, 'verification'])->name('seller.verification.index');
    Route::post('verification/upload', [SellerController::class, 'uploadKyc'])->name('seller.verification.upload');
    
    // Account Status Toggle
    Route::post('account/toggle-status', [SellerController::class, 'toggleStatus'])->name('seller.account.toggle-status');
    
    // Promotion Management
    Route::prefix('promotions')->name('seller.promotions.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Seller\PromotionController::class, 'index'])->name('index');
        Route::get('packages', [\App\Http\Controllers\Seller\PromotionController::class, 'packages'])->name('packages');
        Route::get('select-product/{package}', [\App\Http\Controllers\Seller\PromotionController::class, 'selectProduct'])->name('select-product');
        Route::post('checkout/{package}', [\App\Http\Controllers\Seller\PromotionController::class, 'checkout'])->name('checkout');
        Route::post('payment/initiate', [\App\Http\Controllers\Seller\PromotionController::class, 'initiatePayment'])->name('payment.initiate');
        Route::post('payment/callback', [\App\Http\Controllers\Seller\PromotionController::class, 'paymentCallback'])->name('payment.callback');
        Route::get('payment/success/{transaction}', [\App\Http\Controllers\Seller\PromotionController::class, 'paymentSuccess'])->name('payment.success');
        Route::get('payment/failed/{transaction}', [\App\Http\Controllers\Seller\PromotionController::class, 'paymentFailed'])->name('payment.failed');
        Route::delete('{promotion}', [\App\Http\Controllers\Seller\PromotionController::class, 'destroy'])->name('destroy');
    });
    
    Route::resource('banners', \App\Http\Controllers\Seller\BannerController::class, ['as' => 'seller']);
    
    // My Quotes
    Route::get('quotes', [SellerController::class, 'myQuotes'])->name('seller.quotes.index');
    Route::get('quotes/{quote}', [SellerController::class, 'quoteDetails'])->name('seller.quotes.show');
    Route::post('quotes/{quote}/revise', [SellerController::class, 'reviseQuote'])->name('seller.quotes.revise');
    
    // Microsite Management
    Route::prefix('microsite')->name('seller.microsite.')->group(function () {
        Route::get('settings', [\App\Http\Controllers\Seller\MicrositeController::class, 'settings'])->name('settings');
        Route::post('settings', [\App\Http\Controllers\Seller\MicrositeController::class, 'updateSettings'])->name('settings.update');
        Route::get('testimonials', [\App\Http\Controllers\Seller\MicrositeController::class, 'testimonials'])->name('testimonials');
        Route::post('testimonials', [\App\Http\Controllers\Seller\MicrositeController::class, 'storeTestimonial'])->name('testimonials.store');
        Route::delete('testimonials/{testimonial}', [\App\Http\Controllers\Seller\MicrositeController::class, 'destroyTestimonial'])->name('testimonials.destroy');
        Route::get('faqs', [\App\Http\Controllers\Seller\MicrositeController::class, 'faqs'])->name('faqs');
        Route::post('faqs', [\App\Http\Controllers\Seller\MicrositeController::class, 'storeFaq'])->name('faqs.store');
        Route::delete('faqs/{faq}', [\App\Http\Controllers\Seller\MicrositeController::class, 'destroyFaq'])->name('faqs.destroy');
        Route::delete('brochure', [\App\Http\Controllers\Seller\MicrositeController::class, 'removeBrochure'])->name('brochure.destroy');
        Route::get('analytics', [\App\Http\Controllers\Seller\MicrositeController::class, 'analytics'])->name('analytics');
    });
});

Route::middleware(['auth', 'role:buyer'])->prefix('buyer')->group(function () {
    Route::get('dashboard', [BuyerController::class, 'dashboard'])->name('buyer.dashboard');
    Route::resource('rfqs', \App\Http\Controllers\Buyer\RfqController::class, ['as' => 'buyer'])->only(['index', 'create', 'store', 'show']);
    Route::get('favorites', [BuyerController::class, 'favorites'])->name('buyer.favorites');
    Route::post('favorites/{product}', [BuyerController::class, 'addFavorite'])->name('buyer.favorites.add');
    Route::delete('favorites/{favorite}', [BuyerController::class, 'removeFavorite'])->name('buyer.favorites.remove');
    Route::get('saved-searches', [BuyerController::class, 'savedSearches'])->name('buyer.saved-searches');
    Route::delete('saved-searches/{savedSearch}', [BuyerController::class, 'deleteSavedSearch'])->name('buyer.saved-searches.destroy');
    Route::post('saved-searches', [BuyerController::class, 'saveSearch'])->name('buyer.saved-searches.store');
    
    Route::get('inquiries', [BuyerController::class, 'inquiries'])->name('buyer.inquiries.index');
    Route::get('profile', [BuyerController::class, 'profile'])->name('buyer.profile');
    Route::post('profile', [BuyerController::class, 'updateProfile'])->name('buyer.profile.update');
    
    // Quote Requests
    Route::get('quotes', [BuyerController::class, 'receivedQuotes'])->name('buyer.quotes.index');
    Route::get('quotes/{quote}', [BuyerController::class, 'quoteDetails'])->name('buyer.quotes.show');
    Route::post('quotes/{quote}/respond', [BuyerController::class, 'respondToQuote'])->name('buyer.quotes.respond');
});

Route::middleware(['auth', 'role:admin'])->prefix('admin')->group(function () {
    Route::get('dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('users', [AdminController::class, 'users'])->name('admin.users');
    Route::post('users', [AdminController::class, 'storeUser'])->name('admin.users.store');
    Route::post('users/{user}/toggle-status', [AdminController::class, 'toggleUserStatus'])->name('admin.users.toggle-status');
    Route::post('users/bulk-action', [AdminController::class, 'bulkAction'])->name('admin.users.bulk-action');
    Route::get('users/{user}/edit', [AdminController::class, 'editUser'])->name('admin.users.edit');
    Route::put('users/{user}', [AdminController::class, 'updateUser'])->name('admin.users.update');
    Route::get('sellers', [AdminController::class, 'sellers'])->name('admin.sellers');
    Route::post('sellers/{id}/verify', [AdminController::class, 'verifySeller'])->name('admin.sellers.verify');
    Route::post('sellers/{id}/revoke', [AdminController::class, 'revokeSeller'])->name('admin.sellers.revoke');
    Route::post('sellers/{id}/reject', [AdminController::class, 'rejectSeller'])->name('admin.sellers.reject');
    Route::get('sellers/{id}/edit', [AdminController::class, 'editSeller'])->name('admin.sellers.edit');
    Route::put('sellers/{id}', [AdminController::class, 'updateSeller'])->name('admin.sellers.update');
    Route::post('sellers/{seller}/toggle-status', [AdminController::class, 'toggleSellerStatus'])->name('admin.sellers.toggle-status');
    Route::delete('sellers/{seller}/delete', [AdminController::class, 'deleteSeller'])->name('admin.sellers.delete');
    Route::post('sellers/{seller}/restore', [AdminController::class, 'restoreSeller'])->name('admin.sellers.restore');
    
    // KYC Management
    Route::get('kyc', [\App\Http\Controllers\Admin\KycController::class, 'index'])->name('admin.kyc.index');
    Route::get('kyc/seller/{seller}', [\App\Http\Controllers\Admin\KycController::class, 'show'])->name('admin.kyc.show');
    Route::post('kyc/{document}/approve', [\App\Http\Controllers\Admin\KycController::class, 'approve'])->name('admin.kyc.approve');
    Route::post('kyc/{document}/reject', [\App\Http\Controllers\Admin\KycController::class, 'reject'])->name('admin.kyc.reject');
    Route::post('kyc/{document}/reset', [\App\Http\Controllers\Admin\KycController::class, 'resetToPending'])->name('admin.kyc.reset');
    Route::post('kyc/bulk-approve', [\App\Http\Controllers\Admin\KycController::class, 'bulkApprove'])->name('admin.kyc.bulk-approve');
    Route::post('kyc/bulk-reject', [\App\Http\Controllers\Admin\KycController::class, 'bulkReject'])->name('admin.kyc.bulk-reject');

    Route::get('products', [AdminController::class, 'products'])->name('admin.products');
    Route::post('products/bulk-action', [AdminController::class, 'bulkProductAction'])->name('admin.products.bulk-action');
    Route::post('products/{id}/approve', [AdminController::class, 'approveProduct'])->name('admin.products.approve');
    Route::post('products/{id}/reject', [AdminController::class, 'rejectProduct'])->name('admin.products.reject');
    Route::post('products/{id}/toggle-featured', [AdminController::class, 'toggleFeatured'])->name('admin.products.toggle-featured');
    Route::get('products/{product}/edit', [AdminController::class, 'editProduct'])->name('admin.products.edit');
    Route::put('products/{product}', [AdminController::class, 'updateProduct'])->name('admin.products.update');
    Route::delete('products/{id}', [AdminController::class, 'deleteProduct'])->name('admin.products.delete');
    
    // Impersonation
    Route::get('users/{user}/impersonate', [AdminController::class, 'impersonate'])->name('admin.users.impersonate');
    Route::get('rfqs', [AdminController::class, 'rfqs'])->name('admin.rfqs');
    Route::delete('rfqs/{id}', [AdminController::class, 'deleteRfq'])->name('admin.rfqs.delete');
    
    Route::post('banners/{banner}/toggle', [\App\Http\Controllers\Admin\BannerController::class, 'toggleStatus'])->name('admin.banners.toggle');
    Route::resource('banners', \App\Http\Controllers\Admin\BannerController::class, ['as' => 'admin']);
    Route::resource('categories', \App\Http\Controllers\Admin\CategoryController::class, ['as' => 'admin']);
    Route::resource('units', \App\Http\Controllers\Admin\UnitController::class, ['as' => 'admin']);
    
    // CMS & Blog Admin
    
    // CMS & Blog Admin
    // CMS & Blog Admin
    Route::resource('pages', \App\Http\Controllers\Admin\PageController::class, ['as' => 'admin'])->except(['show']);
    Route::resource('blog', \App\Http\Controllers\Admin\BlogController::class, ['as' => 'admin'])->except(['show']);

    // Review Moderation
    Route::get('reviews', [\App\Http\Controllers\Admin\ReviewController::class, 'index'])->name('admin.reviews.index');
    Route::post('reviews/{review}/approve', [\App\Http\Controllers\Admin\ReviewController::class, 'approve'])->name('admin.reviews.approve');
    Route::delete('reviews/{review}', [\App\Http\Controllers\Admin\ReviewController::class, 'destroy'])->name('admin.reviews.destroy');

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

    // Plans
    Route::resource('plans', \App\Http\Controllers\Admin\PlanController::class, ['as' => 'admin']);
    
    // Subscription Reports
    Route::get('subscriptions/report', [\App\Http\Controllers\Admin\SubscriptionReportController::class, 'index'])->name('admin.subscriptions.report');
    Route::post('subscriptions/{transaction}/approve', [\App\Http\Controllers\Admin\SubscriptionReportController::class, 'approveManualPayment'])->name('admin.subscriptions.approve');
    Route::post('subscriptions/{transaction}/reject', [\App\Http\Controllers\Admin\SubscriptionReportController::class, 'rejectManualPayment'])->name('admin.subscriptions.reject');

    // Broadcast Messages
    Route::get('broadcast', [\App\Http\Controllers\Admin\BroadcastController::class, 'index'])->name('admin.broadcast.index');
    Route::post('broadcast/send', [\App\Http\Controllers\Admin\BroadcastController::class, 'send'])->name('admin.broadcast.send');

    Route::post('email_templates/{emailTemplate}/toggle', [\App\Http\Controllers\Admin\EmailTemplateController::class, 'toggleStatus'])->name('admin.email_templates.toggle');
    Route::post('email_templates/{emailTemplate}/test', [\App\Http\Controllers\Admin\EmailTemplateController::class, 'test'])->name('admin.email_templates.test');
    Route::resource('email_templates', \App\Http\Controllers\Admin\EmailTemplateController::class, ['as' => 'admin'])->except(['show', 'destroy']);
    Route::get('email_logs', [\App\Http\Controllers\Admin\EmailLogController::class, 'index'])->name('admin.email_logs.index');
    
    // Help FAQ Management
    Route::prefix('help-faqs')->name('admin.help-faqs.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Admin\HelpFaqController::class, 'index'])->name('index');
        Route::get('/create', [\App\Http\Controllers\Admin\HelpFaqController::class, 'create'])->name('create');
        Route::post('/', [\App\Http\Controllers\Admin\HelpFaqController::class, 'store'])->name('store');
        Route::get('/{helpFaq}/edit', [\App\Http\Controllers\Admin\HelpFaqController::class, 'edit'])->name('edit');
        Route::put('/{helpFaq}', [\App\Http\Controllers\Admin\HelpFaqController::class, 'update'])->name('update');
        Route::delete('/{helpFaq}', [\App\Http\Controllers\Admin\HelpFaqController::class, 'destroy'])->name('destroy');
        Route::post('/{helpFaq}/toggle', [\App\Http\Controllers\Admin\HelpFaqController::class, 'toggleStatus'])->name('toggle');
    });
    
    // Promotion Management
    Route::prefix('promotions')->name('admin.promotions.')->group(function () {
        Route::get('settings', [\App\Http\Controllers\Admin\PromotionSettingController::class, 'index'])->name('settings');
        Route::put('settings', [\App\Http\Controllers\Admin\PromotionSettingController::class, 'update'])->name('settings.update');
        Route::resource('packages', \App\Http\Controllers\Admin\PromotionPackageController::class)->except(['show']);
    });
    
    // Transaction Management
    Route::prefix('transactions')->name('admin.transactions.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Admin\TransactionController::class, 'index'])->name('index');
        Route::get('/export', [\App\Http\Controllers\Admin\TransactionController::class, 'export'])->name('export');
        Route::post('/{transaction}/approve', [\App\Http\Controllers\Admin\TransactionController::class, 'approve'])->name('approve');
        Route::post('/{transaction}/reject', [\App\Http\Controllers\Admin\TransactionController::class, 'reject'])->name('reject');
        Route::get('/{transaction}', [\App\Http\Controllers\Admin\TransactionController::class, 'show'])->name('show');
    });
    
    // Quote Management
    Route::prefix('quotes')->name('admin.quotes.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Admin\QuoteController::class, 'index'])->name('index');
        Route::get('/export', [\App\Http\Controllers\Admin\QuoteController::class, 'export'])->name('export');
        Route::get('/{quote}', [\App\Http\Controllers\Admin\QuoteController::class, 'show'])->name('show');
    });
    
    // Inquiry Management
    Route::prefix('inquiries')->name('admin.inquiries.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Admin\InquiryController::class, 'index'])->name('index');
        Route::get('/export', [\App\Http\Controllers\Admin\InquiryController::class, 'export'])->name('export');
        Route::post('/{inquiry}/assign', [\App\Http\Controllers\Admin\InquiryController::class, 'assign'])->name('assign');
        Route::post('/{inquiry}/reject', [\App\Http\Controllers\Admin\InquiryController::class, 'reject'])->name('reject');
        Route::post('/bulk-assign', [\App\Http\Controllers\Admin\InquiryController::class, 'bulkAssign'])->name('bulk-assign');
        Route::get('/{inquiry}', [\App\Http\Controllers\Admin\InquiryController::class, 'show'])->name('show');
    });
    // Fix Slugs Route (Temporary)
    Route::get('fix-slugs', function() {
        $data = [];
        
        // 1. Fix Sellers
        $sellers = \App\Models\Seller::whereNull('slug')->get();
        $sCount = 0;
        foreach ($sellers as $seller) {
            $base = $seller->company_name ?: ($seller->user->name ?? 'seller-' . $seller->id);
            $seller->slug = \Illuminate\Support\Str::slug($base);
            $originalSlug = $seller->slug;
            $i = 1;
            while (\App\Models\Seller::where('slug', $seller->slug)->where('id', '!=', $seller->id)->exists()) {
                $seller->slug = $originalSlug . '-' . $i++;
            }
            $seller->save();
            $sCount++;
        }
        $data['sellers'] = $sCount;

        // 2. Fix Products
        $products = \App\Models\Product::whereNull('slug')->get();
        $pCount = 0;
        foreach ($products as $product) {
            $product->slug = \Illuminate\Support\Str::slug($product->name);
            $originalSlug = $product->slug;
            $i = 1;
            while (\App\Models\Product::where('slug', $product->slug)->where('id', '!=', $product->id)->exists()) {
                $product->slug = $originalSlug . '-' . $i++;
            }
            $product->save();
            $pCount++;
        }
        $data['products'] = $pCount;

        // 3. Fix Categories
        $categories = \App\Models\Category::whereNull('slug')->get();
        $cCount = 0;
        foreach ($categories as $category) {
            $category->slug = \Illuminate\Support\Str::slug($category->name);
            $originalSlug = $category->slug;
            $i = 1;
            while (\App\Models\Category::where('slug', $category->slug)->where('id', '!=', $category->id)->exists()) {
                $category->slug = $originalSlug . '-' . $i++;
            }
            $category->save();
            $cCount++;
        }
        $data['categories'] = $cCount;

        return "Fixed Slugs: " . json_encode($data);
    })->name('admin.fix-slugs');
});


// Buyer Review Submission
Route::middleware(['auth', 'role:buyer'])->post('sellers/{seller}/review', [\App\Http\Controllers\ReviewController::class, 'store'])->name('reviews.store');

Route::middleware(['auth', 'role:buyer'])->prefix('buyer')->group(function () {
    Route::get('quotes', [\App\Http\Controllers\Buyer\QuoteController::class, 'index'])->name('buyer.quotes.index');
    Route::post('quotes/{quote}/status', [\App\Http\Controllers\Buyer\QuoteController::class, 'updateStatus'])->name('buyer.quotes.status');
});

// Public Blog Routes
Route::get('/blog', [\App\Http\Controllers\Public\BlogController::class, 'index'])->name('blog.index');
Route::get('/blog/category/{slug}', [\App\Http\Controllers\Public\BlogController::class, 'category'])->name('blog.category');
Route::get('/blog/{slug}', [\App\Http\Controllers\Public\BlogController::class, 'show'])->name('blog.show');

// Public Page Route (Fallback or specific prefix)
Route::get('/p/{slug}', [\App\Http\Controllers\Public\PageController::class, 'show'])->name('page.show');

// Sitemap
Route::get('/sitemap.xml', [\App\Http\Controllers\Public\SitemapController::class, 'index'])->name('sitemap');


// Public Seller Profile (Micro-site)
Route::prefix('s')->group(function () {
    Route::get('/{slug}/products', [\App\Http\Controllers\Public\SellerProfileController::class, 'products'])->name('seller.public.products');
    Route::get('/{slug}', [\App\Http\Controllers\Public\SellerProfileController::class, 'show'])->name('seller.public.profile');
});

// Public Search & Directory
Route::get('/', [\App\Http\Controllers\Public\SearchController::class, 'home'])->name('home');
Route::get('/search', [\App\Http\Controllers\Public\SearchController::class, 'index'])->name('public.search');
Route::get('/ajax/search-autocomplete', [\App\Http\Controllers\Public\SearchController::class, 'autocomplete'])->name('public.search.autocomplete');
Route::get('/products', [\App\Http\Controllers\Public\SearchController::class, 'products'])->name('public.products.index');
Route::get('/products/{slug}', [\App\Http\Controllers\Public\ProductController::class, 'show'])->name('public.products.show');
Route::get('/products/{slug}/quick-view', [\App\Http\Controllers\Public\ProductController::class, 'quickView'])->name('public.products.quick-view');
Route::post('/inquiries/{seller}', [\App\Http\Controllers\Public\InquiryController::class, 'store'])->name('inquiries.store');
Route::get('/suppliers', [\App\Http\Controllers\Public\SearchController::class, 'suppliers'])->name('public.suppliers.index');

// Category Routes
Route::get('/categories', [\App\Http\Controllers\Public\CategoryController::class, 'index'])->name('public.categories.index');
Route::get('/category/{slug}', [\App\Http\Controllers\Public\CategoryController::class, 'show'])->name('public.categories.show');

// Duplicate route removed - using /inquiries/{seller} instead

// Route::get('products', [ProductController::class, 'index']); // Removed conflict
// Route::get('products/{product}', [ProductController::class, 'show']); // Removed conflict

// Post Requirement Routes
Route::get('/post-requirement', [App\Http\Controllers\Public\PostRequirementController::class, 'index'])->name('post.requirement.index');
Route::post('/post-requirement', [App\Http\Controllers\Public\PostRequirementController::class, 'store'])->name('post.requirement.store');

// Public Buy Leads (RFQs)
Route::get('/buy-leads', [\App\Http\Controllers\Public\RfqController::class, 'index'])->name('public.rfqs.index');
Route::get('/buy-leads/{id}', [\App\Http\Controllers\Public\RfqController::class, 'show'])->name('public.rfqs.show');
Route::post('/buy-leads/{rfq}/quote', [\App\Http\Controllers\Public\QuoteController::class, 'store'])->name('public.rfqs.quote')->middleware(['auth', 'role:seller']);
// Newsletter
Route::post('/newsletter/subscribe', [\App\Http\Controllers\Public\NewsletterController::class, 'subscribe'])->name('newsletter.subscribe');

// Subscription Plans
Route::get('/subscription-plans', [\App\Http\Controllers\Public\SubscriptionController::class, 'plans'])->name('public.subscription.plans');



// --- DEPLOYMENT HELPER (DELETE AFTER USE) ---
Route::get('/deploy-setup', function () {
    try {
        // 1. Link Storage
        \Illuminate\Support\Facades\Artisan::call('storage:link');
        $output = "Storage Linked.<br>";

        // 2. Run Migrations
        \Illuminate\Support\Facades\Artisan::call('migrate', ['--force' => true]);
        $output .= "Database Migrated.<br>";

        // 3. Clear & Cache Config
        \Illuminate\Support\Facades\Artisan::call('config:clear');
        \Illuminate\Support\Facades\Artisan::call('cache:clear');
        \Illuminate\Support\Facades\Artisan::call('view:clear');
        \Illuminate\Support\Facades\Artisan::call('route:clear');
        $output .= "Cache Cleared.<br>";
        
        return "<h1>Setup Complete! 🚀</h1><p>" . $output . "</p><p>You can now delete this route from web.php</p>";
    } catch (\Exception $e) {
        return "<h1>Error! ❌</h1><p>" . $e->getMessage() . "</p>";
    }
});

