<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\AdminRoleController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\AnnouncementController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\ContentPageController;
use App\Http\Controllers\Admin\CouponController;
use App\Http\Controllers\Admin\EmailTemplateController;
use App\Http\Controllers\Admin\HomeSliderController;
use App\Http\Controllers\Admin\InventoryController;
use App\Http\Controllers\Admin\NewsletterSubscriberController;
use App\Http\Controllers\Admin\OrderController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SettingController;
use Illuminate\Support\Facades\Route;

Route::prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::middleware('guest:admin')->group(function () {
            Route::get('/login', [LoginController::class, 'create'])->name('login');
            Route::post('/login', [LoginController::class, 'store'])->name('login.store');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:dashboard.view'])->group(function () {
            Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
            Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:products.manage'])->group(function () {
            Route::get('/categories', [CategoryController::class, 'index'])->name('categories.index');
            Route::get('/categories/create', [CategoryController::class, 'create'])->name('categories.create');
            Route::post('/categories', [CategoryController::class, 'store'])->name('categories.store');
            Route::get('/categories/{category}/edit', [CategoryController::class, 'edit'])->name('categories.edit');
            Route::patch('/categories/{category}', [CategoryController::class, 'update'])->name('categories.update');
            Route::patch('/categories/{category}/toggle', [CategoryController::class, 'toggle'])->name('categories.toggle');
            Route::delete('/categories/{category}', [CategoryController::class, 'destroy'])->name('categories.destroy');

            Route::get('/products', [ProductController::class, 'index'])->name('products.index');
            Route::get('/products/create', [ProductController::class, 'create'])->name('products.create');
            Route::post('/products', [ProductController::class, 'store'])->name('products.store');
            Route::get('/products/{product}/edit', [ProductController::class, 'edit'])->name('products.edit');
            Route::patch('/products/{product}', [ProductController::class, 'update'])->name('products.update');
            Route::patch('/products/{product}/toggle', [ProductController::class, 'toggle'])->name('products.toggle');
            Route::delete('/products/{product}', [ProductController::class, 'destroy'])->name('products.destroy');

            Route::get('/inventory', [InventoryController::class, 'index'])->name('inventory.index');

            Route::get('/home-sliders', [HomeSliderController::class, 'index'])->name('home-sliders.index');
            Route::get('/home-sliders/create', [HomeSliderController::class, 'create'])->name('home-sliders.create');
            Route::post('/home-sliders', [HomeSliderController::class, 'store'])->name('home-sliders.store');
            Route::get('/home-sliders/{homeSlider}/edit', [HomeSliderController::class, 'edit'])->name('home-sliders.edit');
            Route::patch('/home-sliders/{homeSlider}', [HomeSliderController::class, 'update'])->name('home-sliders.update');
            Route::patch('/home-sliders/{homeSlider}/toggle', [HomeSliderController::class, 'toggle'])->name('home-sliders.toggle');
            Route::delete('/home-sliders/{homeSlider}', [HomeSliderController::class, 'destroy'])->name('home-sliders.destroy');

            Route::get('/announcements', [AnnouncementController::class, 'index'])->name('announcements.index');
            Route::get('/announcements/create', [AnnouncementController::class, 'create'])->name('announcements.create');
            Route::post('/announcements', [AnnouncementController::class, 'store'])->name('announcements.store');
            Route::get('/announcements/{announcement}/edit', [AnnouncementController::class, 'edit'])->name('announcements.edit');
            Route::patch('/announcements/{announcement}', [AnnouncementController::class, 'update'])->name('announcements.update');
            Route::patch('/announcements/{announcement}/toggle', [AnnouncementController::class, 'toggle'])->name('announcements.toggle');
            Route::delete('/announcements/{announcement}', [AnnouncementController::class, 'destroy'])->name('announcements.destroy');

            Route::get('/newsletter-subscribers', [NewsletterSubscriberController::class, 'index'])->name('newsletter-subscribers.index');
            Route::patch('/newsletter-subscribers/{subscriber}/toggle', [NewsletterSubscriberController::class, 'toggle'])->name('newsletter-subscribers.toggle');
            Route::delete('/newsletter-subscribers/{subscriber}', [NewsletterSubscriberController::class, 'destroy'])->name('newsletter-subscribers.destroy');

            Route::get('/email-templates', [EmailTemplateController::class, 'index'])->name('email-templates.index');
            Route::get('/email-templates/create', [EmailTemplateController::class, 'create'])->name('email-templates.create');
            Route::post('/email-templates', [EmailTemplateController::class, 'store'])->name('email-templates.store');
            Route::get('/email-templates/{emailTemplate}/edit', [EmailTemplateController::class, 'edit'])->name('email-templates.edit');
            Route::patch('/email-templates/{emailTemplate}', [EmailTemplateController::class, 'update'])->name('email-templates.update');
            Route::delete('/email-templates/{emailTemplate}', [EmailTemplateController::class, 'destroy'])->name('email-templates.destroy');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:settings.manage'])->group(function () {
            Route::get('/settings', [SettingController::class, 'edit'])->name('settings.edit');
            Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');
            Route::post('/settings/test-email', [SettingController::class, 'testEmail'])->name('settings.test-email');
            Route::post('/settings/generate-sitemap', [SettingController::class, 'generateSitemap'])->name('settings.generate-sitemap');
            Route::get('/content-pages', [ContentPageController::class, 'index'])->name('content-pages.index');
            Route::get('/content-pages/{page}/edit', [ContentPageController::class, 'edit'])->name('content-pages.edit');
            Route::patch('/content-pages/{page}', [ContentPageController::class, 'update'])->name('content-pages.update');
            Route::get('/coupons', [CouponController::class, 'index'])->name('coupons.index');
            Route::get('/coupons/create', [CouponController::class, 'create'])->name('coupons.create');
            Route::post('/coupons', [CouponController::class, 'store'])->name('coupons.store');
            Route::get('/coupons/{coupon}/edit', [CouponController::class, 'edit'])->name('coupons.edit');
            Route::patch('/coupons/{coupon}', [CouponController::class, 'update'])->name('coupons.update');
            Route::patch('/coupons/{coupon}/toggle', [CouponController::class, 'toggle'])->name('coupons.toggle');
            Route::delete('/coupons/{coupon}', [CouponController::class, 'destroy'])->name('coupons.destroy');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:customers.manage'])->group(function () {
            Route::get('/customers', [CustomerController::class, 'index'])->name('customers.index');
            Route::get('/customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
            Route::patch('/customers/{customer}/toggle', [CustomerController::class, 'toggle'])->name('customers.toggle');
            Route::delete('/customers/{customer}', [CustomerController::class, 'destroy'])->name('customers.destroy');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:admin-users.manage'])->group(function () {
            Route::get('/admin-users', [AdminUserController::class, 'index'])->name('admin-users.index');
            Route::get('/admin-users/create', [AdminUserController::class, 'create'])->name('admin-users.create');
            Route::post('/admin-users', [AdminUserController::class, 'store'])->name('admin-users.store');
            Route::get('/admin-users/{adminUser}/edit', [AdminUserController::class, 'edit'])->name('admin-users.edit');
            Route::patch('/admin-users/{adminUser}', [AdminUserController::class, 'update'])->name('admin-users.update');
            Route::patch('/admin-users/{adminUser}/toggle', [AdminUserController::class, 'toggle'])->name('admin-users.toggle');
            Route::delete('/admin-users/{adminUser}', [AdminUserController::class, 'destroy'])->name('admin-users.destroy');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:roles.manage'])->group(function () {
            Route::get('/roles', [AdminRoleController::class, 'index'])->name('roles.index');
            Route::get('/roles/create', [AdminRoleController::class, 'create'])->name('roles.create');
            Route::post('/roles', [AdminRoleController::class, 'store'])->name('roles.store');
            Route::get('/roles/{role}/edit', [AdminRoleController::class, 'edit'])->name('roles.edit');
            Route::patch('/roles/{role}', [AdminRoleController::class, 'update'])->name('roles.update');
            Route::delete('/roles/{role}', [AdminRoleController::class, 'destroy'])->name('roles.destroy');
        });

        Route::middleware(['auth:admin', 'admin.active', 'admin.permission:orders.manage'])->group(function () {
            Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
            Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
            Route::get('/reports/low-stock-export', [ReportController::class, 'exportLowStock'])->name('reports.low-stock-export');
            Route::get('/orders/{order}', [OrderController::class, 'show'])->name('orders.show');
            Route::get('/orders/{order}/invoice', [OrderController::class, 'invoice'])->name('orders.invoice');
            Route::patch('/orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.status');
            Route::patch('/orders/{order}/approve', [OrderController::class, 'approve'])->name('orders.approve');
            Route::patch('/orders/{order}/reject', [OrderController::class, 'reject'])->name('orders.reject');
            Route::patch('/orders/{order}/process', [OrderController::class, 'process'])->name('orders.process');
            Route::patch('/orders/{order}/complete', [OrderController::class, 'complete'])->name('orders.complete');
            Route::patch('/order-complaints/{complaint}/response', [OrderController::class, 'respondToComplaint'])->name('order-complaints.response');
        });
    });
