<?php

use App\Http\Controllers\Admin\AnalyticsController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\AdminReviewController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AdminSettingsController;
use App\Http\Controllers\Admin\BackupController;
use App\Http\Controllers\Admin\DiscountCodeController;
use App\Http\Controllers\Admin\OrderCancelController;
use App\Http\Controllers\Admin\OrderController;
use App\Http\Controllers\Admin\ShippingMethodController;
use App\Http\Controllers\Admin\TicketController as AdminTicketController;
use App\Http\Controllers\Admin\TicketDepartmentController;
use App\Http\Controllers\Admin\TransactionController;
use App\Http\Controllers\Admin\WalletController;
use App\Http\Controllers\Admin\WooImportController;
use App\Http\Controllers\Admin\AnnouncementController;
use App\Http\Controllers\Admin\BiataBotChatController;
use App\Http\Controllers\Admin\BlogCategoryController;
use App\Http\Controllers\Admin\BlogPostController;
use App\Http\Controllers\Admin\BlogTagController;
use App\Http\Controllers\Admin\BrandController;
use App\Http\Controllers\Admin\ProductCategoryController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\AdminSearchController;
use Illuminate\Support\Facades\Route;

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {

    Route::get('/', [AdminController::class, 'index'])->name('index');
    Route::get('/stats.json', [AdminController::class, 'statsJson'])->name('stats.json');
    Route::get('/search', AdminSearchController::class)->name('search');
    Route::get('/analytics', [AnalyticsController::class, 'index'])->name('analytics.index');

    // Users
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/',                    [AdminUserController::class, 'index'])->name('index');
        Route::get('/{user:mobile}',       [AdminUserController::class, 'show'])->name('show');
        Route::put('/{user:mobile}',       [AdminUserController::class, 'update'])->middleware('persian.digits')->name('update');
        Route::post('/{user:mobile}/toggle-status', [AdminUserController::class, 'toggleStatus'])->name('toggle-status');
        Route::post('/{user:mobile}/make-admin', [AdminUserController::class, 'makeAdmin'])->name('make-admin');
    });

    // Orders
    Route::get('orders', [OrderController::class, 'index'])->name('orders.index');
    Route::post('orders/amadast-sync', [OrderController::class, 'amadastSync'])->name('orders.amadast-sync');
    Route::delete('orders/bulk', [OrderController::class, 'bulkDestroy'])->name('orders.bulk-destroy');
    Route::get('orders/returns', [OrderCancelController::class, 'index'])->name('orders.returns.index');
    Route::get('orders/returns/{order}', [OrderCancelController::class, 'show'])->name('orders.returns.show');
    Route::post('orders/returns/{order}', [OrderCancelController::class, 'decide'])->name('orders.returns.decide');
    Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');
    Route::post('orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.updateStatus');
    Route::post('orders/{order}/tracking', [OrderController::class, 'saveTracking'])->name('orders.saveTracking');
    Route::delete('orders/{order}', [OrderController::class, 'destroy'])->name('orders.destroy');

    // Product categories
    Route::prefix('product-categories')->name('products.categories.')->group(function () {
        Route::get('/',          [ProductCategoryController::class, 'index'])->name('index');
        Route::get('/new',       [ProductCategoryController::class, 'create'])->name('create');
        Route::post('/',         [ProductCategoryController::class, 'store'])->name('store');
        Route::get('/{category}/edit',  [ProductCategoryController::class, 'edit'])->name('edit');
        Route::put('/{category}',       [ProductCategoryController::class, 'update'])->name('update');
        Route::delete('/{category}',    [ProductCategoryController::class, 'destroy'])->name('destroy');
        Route::post('/reorder',  [ProductCategoryController::class, 'reorder'])->name('reorder');
    });

    // Products
    Route::prefix('products')->name('products.')->group(function () {
        Route::get('/',               [ProductController::class, 'index'])->name('index');
        Route::get('/search',        [ProductController::class, 'searchSimilar'])->name('search');
        Route::get('/new',            [ProductController::class, 'create'])->name('create');
        Route::post('/ai-generate',   [ProductController::class, 'aiGenerate'])->name('ai-generate');
        Route::get('/ai-generate/progress/{jobId}', [ProductController::class, 'aiGenerateProgress'])->name('ai-generate.progress');
        Route::post('/',              [ProductController::class, 'store'])->name('store');
        Route::get('/{product}/edit', [ProductController::class, 'edit'])->name('edit');
        Route::put('/{product}',      [ProductController::class, 'update'])->name('update');
        Route::delete('/{product}',   [ProductController::class, 'destroy'])->name('destroy');
    });

    // Reviews
    Route::prefix('reviews')->name('reviews.')->group(function () {
        Route::get('/',                    [AdminReviewController::class, 'index'])->name('index');
        Route::get('/new',                 [AdminReviewController::class, 'create'])->name('create');
        Route::post('/',                   [AdminReviewController::class, 'store'])->name('store');
        Route::get('/{review}/edit',       [AdminReviewController::class, 'edit'])->name('edit');
        Route::put('/{review}',            [AdminReviewController::class, 'update'])->name('update');
        Route::delete('/{review}',         [AdminReviewController::class, 'destroy'])->name('destroy');
        Route::patch('/{review}/approve',  [AdminReviewController::class, 'approve'])->name('approve');
        Route::patch('/{review}/reject',   [AdminReviewController::class, 'reject'])->name('reject');
    });

    // Brands
    Route::prefix('brands')->name('brands.')->group(function () {
        Route::get('/',                  [BrandController::class, 'index'])->name('index');
        Route::get('/new',               [BrandController::class, 'create'])->name('create');
        Route::post('/',                 [BrandController::class, 'store'])->name('store');
        Route::post('/quick-create',     [BrandController::class, 'quickCreate'])->name('quick-create');
        Route::get('/{brand}/edit',      [BrandController::class, 'edit'])->name('edit');
        Route::put('/{brand}',           [BrandController::class, 'update'])->name('update');
        Route::delete('/{brand}',        [BrandController::class, 'destroy'])->name('destroy');
    });

    // Blog
    Route::prefix('blog')->name('blog.')->group(function () {

        // Categories
        Route::prefix('categories')->name('categories.')->group(function () {
            Route::get('/',                  [BlogCategoryController::class, 'index'])->name('index');
            Route::get('/new',               [BlogCategoryController::class, 'create'])->name('create');
            Route::post('/',                 [BlogCategoryController::class, 'store'])->name('store');
            Route::get('/{category}/edit',   [BlogCategoryController::class, 'edit'])->name('edit');
            Route::put('/{category}',        [BlogCategoryController::class, 'update'])->name('update');
            Route::delete('/{category}',     [BlogCategoryController::class, 'destroy'])->name('destroy');
        });

        // Tags
        Route::prefix('tags')->name('tags.')->group(function () {
            Route::get('/',              [BlogTagController::class, 'index'])->name('index');
            Route::get('/new',           [BlogTagController::class, 'create'])->name('create');
            Route::post('/',             [BlogTagController::class, 'store'])->name('store');
            Route::get('/{tag}/edit',    [BlogTagController::class, 'edit'])->name('edit');
            Route::put('/{tag}',         [BlogTagController::class, 'update'])->name('update');
            Route::delete('/{tag}',      [BlogTagController::class, 'destroy'])->name('destroy');
        });

        // Posts
        Route::prefix('posts')->name('posts.')->group(function () {
            Route::get('/',                  [BlogPostController::class, 'index'])->name('index');
            Route::get('/new',               [BlogPostController::class, 'create'])->name('create');
            Route::post('/',                 [BlogPostController::class, 'store'])->name('store');
            Route::get('/{post}/edit',       [BlogPostController::class, 'edit'])->name('edit');
            Route::put('/{post}',            [BlogPostController::class, 'update'])->name('update');
            Route::delete('/{post}',         [BlogPostController::class, 'destroy'])->name('destroy');
            Route::post('/media/upload',     [BlogPostController::class, 'uploadMedia'])->name('media.upload');
            Route::post('/ai-generate',      [BlogPostController::class, 'aiGenerate'])->name('ai-generate');
            Route::get('/ai-generate/progress/{jobId}', [BlogPostController::class, 'aiGenerateProgress'])->name('ai-generate.progress');
        });
    });

    // Announcements
    Route::prefix('announcements')->name('announcements.')->group(function () {
        Route::get('/',                     [AnnouncementController::class, 'index'])->name('index');
        Route::get('/new',                  [AnnouncementController::class, 'create'])->name('create');
        Route::post('/',                    [AnnouncementController::class, 'store'])->name('store');
        Route::get('/{announcement}/edit',  [AnnouncementController::class, 'edit'])->name('edit');
        Route::put('/{announcement}',       [AnnouncementController::class, 'update'])->name('update');
        Route::delete('/{announcement}',    [AnnouncementController::class, 'destroy'])->name('destroy');
        Route::post('/media/upload',        [AnnouncementController::class, 'uploadMedia'])->name('media.upload');
    });

    // Settings
    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('/main',                [AdminSettingsController::class, 'main'])->name('main');
        Route::post('/main',               [AdminSettingsController::class, 'mainSave'])->name('main.save');
        Route::get('/torob-api',           [AdminSettingsController::class, 'torobApi'])->name('torob-api');
        Route::post('/torob-api',          [AdminSettingsController::class, 'torobApiSave'])->name('torob-api.save');
        Route::get('/appearance',          [AdminSettingsController::class, 'appearance'])->name('appearance');
        Route::post('/appearance',         [AdminSettingsController::class, 'appearanceSave'])->name('appearance.save');
        Route::get('/seo',                 [AdminSettingsController::class, 'seo'])->name('seo');
        Route::post('/seo',                [AdminSettingsController::class, 'seoSave'])->name('seo.save');
        Route::get('/sms',                 [AdminSettingsController::class, 'sms'])->name('sms');
        Route::post('/sms',                [AdminSettingsController::class, 'smsSave'])->name('sms.save');
        Route::get('/sms/credit',          [AdminSettingsController::class, 'smsCredit'])->name('sms.credit');
        Route::get('/sms-sending',         [AdminSettingsController::class, 'smsSending'])->name('sms-sending');
        Route::post('/sms-sending',        [AdminSettingsController::class, 'smsSendingSave'])->name('sms-sending.save');
        Route::get('/contact-page',        [AdminSettingsController::class, 'contactPage'])->name('contact-page');
        Route::post('/contact-page',       [AdminSettingsController::class, 'contactPageSave'])->name('contact-page.save');
        Route::get('/about-page',          [AdminSettingsController::class, 'aboutPage'])->name('about-page');
        Route::post('/about-page',         [AdminSettingsController::class, 'aboutPageSave'])->name('about-page.save');
        Route::get('/terms-page',          [AdminSettingsController::class, 'termsPage'])->name('terms-page');
        Route::post('/terms-page',         [AdminSettingsController::class, 'termsPageSave'])->name('terms-page.save');
        Route::post('/terms-page/media',   [AdminSettingsController::class, 'termsPageMediaUpload'])->name('terms-page.media.upload');
        Route::get('/ai-models',           [AdminSettingsController::class, 'aiModels'])->name('ai-models');
        Route::post('/ai-models',          [AdminSettingsController::class, 'aiModelsSave'])->name('ai-models.save');
        Route::get('/ai-models/credit',    [AdminSettingsController::class, 'aiModelsCredit'])->name('ai-models.credit');
        Route::get('/content-generation',  [AdminSettingsController::class, 'contentGeneration'])->name('content-generation');
        Route::post('/content-generation', [AdminSettingsController::class, 'contentGenerationSave'])->name('content-generation.save');
        Route::get('/biata-bot',           [AdminSettingsController::class, 'biataBot'])->name('biata-bot');
        Route::post('/biata-bot',          [AdminSettingsController::class, 'biataBotSave'])->name('biata-bot.save');

        // Database backups (PHP-only dump, no shell)
        Route::get('/backups',                    [BackupController::class, 'index'])->name('backups');
        Route::post('/backups',                   [BackupController::class, 'store'])->name('backups.store');
        Route::get('/backups/{filename}/download',[BackupController::class, 'download'])->name('backups.download');
        Route::delete('/backups/{filename}',      [BackupController::class, 'destroy'])->name('backups.destroy');

        // WooCommerce migration
        Route::get('/woo-import',                       [WooImportController::class, 'index'])->name('woo-import');
        Route::post('/woo-import',                      [WooImportController::class, 'save'])->name('woo-import.save');
        Route::post('/woo-import/test',                 [WooImportController::class, 'testConnection'])->name('woo-import.test');
        Route::get('/woo-import/counts',                [WooImportController::class, 'counts'])->name('woo-import.counts');
        Route::post('/woo-import/categories',           [WooImportController::class, 'importCategories'])->name('woo-import.categories');
        Route::post('/woo-import/products-batch',       [WooImportController::class, 'importProductsBatch'])->name('woo-import.products-batch');
        Route::get('/amadast',             [AdminSettingsController::class, 'amadast'])->name('amadast');
        Route::post('/amadast',            [AdminSettingsController::class, 'amadastSave'])->name('amadast.save');
        Route::get('/payment-methods',     [AdminSettingsController::class, 'paymentMethods'])->name('payment-methods');
        Route::post('/payment-methods',    [AdminSettingsController::class, 'paymentMethodsSave'])->name('payment-methods.save');
        Route::get('/card-to-card',        [AdminSettingsController::class, 'cardToCard'])->name('card-to-card');
        Route::post('/card-to-card',       [AdminSettingsController::class, 'cardToCardSave'])->name('card-to-card.save');
        Route::get('/zibal',               [AdminSettingsController::class, 'zibal'])->name('zibal');
        Route::post('/zibal',              [AdminSettingsController::class, 'zibalSave'])->name('zibal.save');
        Route::get('/torobpay',            [AdminSettingsController::class, 'torobpay'])->name('torobpay');
        Route::post('/torobpay',           [AdminSettingsController::class, 'torobpaySave'])->name('torobpay.save');
        Route::post('/torobpay/campaign',  [AdminSettingsController::class, 'torobpayCampaignSubmit'])->name('torobpay.campaign.submit');
        Route::post('/torobpay/campaign/reset', [AdminSettingsController::class, 'torobpayCampaignReset'])->name('torobpay.campaign.reset');
        Route::get('/torobpay/orders',     [\App\Http\Controllers\Admin\TorobPayAdminController::class, 'orders'])->name('torobpay.orders');
        Route::post('/torobpay/orders/refresh', [\App\Http\Controllers\Admin\TorobPayAdminController::class, 'refresh'])->name('torobpay.orders.refresh');
        Route::post('/torobpay/orders/refund',  [\App\Http\Controllers\Admin\TorobPayAdminController::class, 'refund'])->name('torobpay.orders.refund');
        Route::post('/torobpay/orders/export',  [\App\Http\Controllers\Admin\TorobPayAdminController::class, 'export'])->name('torobpay.orders.export');
        Route::get('/torobpay/support',    [\App\Http\Controllers\Admin\TorobPayAdminController::class, 'support'])->name('torobpay.support');
        Route::get('/digipay',             [AdminSettingsController::class, 'digipay'])->name('digipay');
        Route::post('/digipay',            [AdminSettingsController::class, 'digipaySave'])->name('digipay.save');
        Route::get('/digipay/orders',      [\App\Http\Controllers\Admin\DigiPayAdminController::class, 'orders'])->name('digipay.orders');
        Route::post('/digipay/orders/refund', [\App\Http\Controllers\Admin\DigiPayAdminController::class, 'refund'])->name('digipay.orders.refund');
        Route::get('/digipay/support',     [\App\Http\Controllers\Admin\DigiPayAdminController::class, 'support'])->name('digipay.support');
        Route::get('/club/wheel',          [AdminSettingsController::class, 'clubWheel'])->name('club.wheel');
        Route::post('/club/wheel',         [AdminSettingsController::class, 'clubWheelSave'])->name('club.wheel.save');
        Route::post('/amadast/otp-request',[AdminSettingsController::class, 'amadastOtpRequest'])->name('amadast.otp.request');
        Route::post('/amadast/otp-verify', [AdminSettingsController::class, 'amadastOtpVerify'])->name('amadast.otp.verify');
        Route::post('/amadast/otp-resend',      [AdminSettingsController::class, 'amadastOtpResend'])->name('amadast.otp.resend');
        Route::post('/amadast/logout',          [AdminSettingsController::class, 'amadastLogout'])->name('amadast.logout');
        Route::get('/amadast/api-provinces',              [AdminSettingsController::class, 'apiProvinces'])->name('amadast.api-provinces');
        Route::get('/amadast/api-cities/{provinceId}',    [AdminSettingsController::class, 'apiCities'])->name('amadast.api-cities');
        Route::get('/amadast/product-types',              [AdminSettingsController::class, 'getProductTypes'])->name('amadast.product-types');
        Route::get('/amadast/api-stores',                 [AdminSettingsController::class, 'apiStores'])->name('amadast.api-stores');
        Route::post('/amadast/save-store',                [AdminSettingsController::class, 'saveStore'])->name('amadast.save-store');

        // Shipping Methods
        Route::prefix('shipping-methods')->name('shipping-methods.')->group(function () {
            Route::get('/',                        [ShippingMethodController::class, 'index'])->name('index');
            Route::get('/new',                     [ShippingMethodController::class, 'create'])->name('create');
            Route::post('/',                       [ShippingMethodController::class, 'store'])->name('store');
            Route::get('/{shippingMethod}/edit',   [ShippingMethodController::class, 'edit'])->name('edit');
            Route::put('/{shippingMethod}',        [ShippingMethodController::class, 'update'])->name('update');
            Route::delete('/{shippingMethod}',     [ShippingMethodController::class, 'destroy'])->name('destroy');
            Route::post('/{shippingMethod}/toggle',[ShippingMethodController::class, 'toggleActive'])->name('toggle');
        });
    });

    // Biata bot chat reports
    Route::prefix('biata-bot/chats')->name('biata-bot.chats.')->group(function () {
        Route::get('/', [BiataBotChatController::class, 'index'])->name('index');
        Route::get('/{conversation}', [BiataBotChatController::class, 'show'])->name('show');
        Route::get('/{conversation}/analyze', [BiataBotChatController::class, 'analyze'])->name('analyze');
        Route::delete('/{conversation}', [BiataBotChatController::class, 'destroy'])->name('destroy');
    });

    // Discount codes
    Route::prefix('discount-codes')->name('discount-codes.')->group(function () {
        Route::get('/',                       [DiscountCodeController::class, 'index'])->name('index');
        Route::get('/new',                    [DiscountCodeController::class, 'create'])->name('create');
        Route::post('/',                      [DiscountCodeController::class, 'store'])->name('store');
        Route::get('/{discountCode}/edit',    [DiscountCodeController::class, 'edit'])->name('edit');
        Route::put('/{discountCode}',         [DiscountCodeController::class, 'update'])->name('update');
        Route::delete('/{discountCode}',      [DiscountCodeController::class, 'destroy'])->name('destroy');
        Route::post('/{discountCode}/toggle', [DiscountCodeController::class, 'toggle'])->name('toggle');
    });

    // Wallets
    Route::prefix('wallets')->name('wallets.')->group(function () {
        Route::get('/',                  [WalletController::class, 'index'])->name('index');
        Route::get('/{user}',            [WalletController::class, 'show'])->name('show');
        Route::post('/{user}/adjust',    [WalletController::class, 'adjust'])->name('adjust');
    });

    // Finance / gateway transactions
    Route::prefix('finance')->name('finance.')->group(function () {
        Route::get('/transactions',              [TransactionController::class, 'index'])->name('transactions.index');
        Route::delete('/transactions/bulk',      [TransactionController::class, 'bulkDestroy'])->name('transactions.bulk-destroy');
        Route::get('/transactions/{transaction}', [TransactionController::class, 'show'])->name('transactions.show');
        Route::delete('/transactions/{transaction}', [TransactionController::class, 'destroy'])->name('transactions.destroy');
    });

    Route::get('/profile/edit',    fn() => abort(404))->name('profile.edit');

    // Support tickets
    Route::prefix('tickets')->name('tickets.')->group(function () {

        // Departments (defined before {ticket} to avoid conflicts)
        Route::prefix('departments')->name('departments.')->group(function () {
            Route::get('/',                    [TicketDepartmentController::class, 'index'])->name('index');
            Route::get('/new',                 [TicketDepartmentController::class, 'create'])->name('new');
            Route::post('/',                   [TicketDepartmentController::class, 'store'])->name('store');
            Route::get('/{department}/edit',   [TicketDepartmentController::class, 'edit'])->name('edit');
            Route::put('/{department}',        [TicketDepartmentController::class, 'update'])->name('update');
            Route::delete('/{department}',     [TicketDepartmentController::class, 'destroy'])->name('destroy');
        });

        Route::get('/',                        [AdminTicketController::class, 'index'])->name('index');
        Route::get('/new',                     [AdminTicketController::class, 'create'])->name('new');
        Route::post('/',                       [AdminTicketController::class, 'store'])->name('store');
        Route::get('/{ticket}',                [AdminTicketController::class, 'show'])->name('show');
        Route::post('/{ticket}/reply',         [AdminTicketController::class, 'reply'])->name('reply');
        Route::post('/{ticket}/status',        [AdminTicketController::class, 'updateStatus'])->name('status');
        Route::post('/{ticket}/close',         [AdminTicketController::class, 'close'])->name('close');
        Route::delete('/{ticket}',             [AdminTicketController::class, 'destroy'])->name('delete');
    });

});
