<?php

use App\Http\Controllers\Admin\AdminCategoryController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminFaqController;
use App\Http\Controllers\Admin\AdminOrderController;
use App\Http\Controllers\Admin\AdminProductController;
use App\Http\Controllers\Admin\AdminProfileController;
use App\Http\Controllers\Admin\AdminSettingController;
use App\Http\Controllers\Admin\AdminSlideController;
use App\Http\Controllers\Admin\AdminSubcategoryController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AdminCustomerController;
use App\Http\Controllers\Admin\AdminCustomerAddressController;
use App\Http\Controllers\Admin\AdminPaymentController;
use App\Http\Controllers\Admin\AdminPaymentMethodController;
use App\Http\Controllers\Admin\AdminPermissionController;
use App\Http\Controllers\Admin\AdminReportController;
use App\Http\Controllers\Admin\AdminRoleController;
use App\Http\Controllers\Admin\AdminShippingMethodController;
use App\Http\Controllers\Admin\AdminBrandController;
use App\Http\Controllers\Admin\AdminUnitController;
use App\Http\Controllers\Admin\PageContentController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\UploadController;
use Illuminate\Support\Facades\Route;


Route::get('/', function () {
    return redirect()->route('admin.login');
});

// Admin Authentication Routes
Route::get('/admin/login', [AuthenticatedSessionController::class, 'create'])->middleware('guest')->name('admin.login');
Route::post('/admin/login', [AuthenticatedSessionController::class, 'store'])->middleware('guest');

// Protected Admin Routes
Route::middleware(['auth', 'admin', 'spatie_permission'])->prefix('admin')->name('admin.')->group(function () {
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');

    // Dashboard
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->middleware('module:dashboard')->name('dashboard');
    Route::get('/dashboard/stats', [AdminDashboardController::class, 'getStats'])->middleware('module:dashboard')->name('dashboard.stats');

    // Image Upload
    Route::post('/upload/image', [UploadController::class, 'upload'])->name('upload.image');

    // Profile (Current User)
    Route::get('/profile', [AdminProfileController::class, 'show'])->name('profile.show');
    Route::get('/profile/edit', [AdminProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [AdminProfileController::class, 'update'])->name('profile.update');

    // Users
    Route::resource('users', AdminUserController::class)->middleware('module:staff');
    Route::get('/permissions', [AdminPermissionController::class, 'index'])->middleware('module:staff')->name('permissions.index');
    Route::get('/permissions/{user}/edit', [AdminPermissionController::class, 'edit'])->middleware('module:staff')->name('permissions.edit');
    Route::put('/permissions/{user}', [AdminPermissionController::class, 'update'])->middleware('module:staff')->name('permissions.update');
    Route::resource('roles', AdminRoleController::class)->middleware('module:staff');

    // Customers
    Route::get('/customers/export', [AdminCustomerController::class, 'export'])->name('customers.export')->middleware('module:customers');
    Route::post('/customers/{customer}/toggle-status', [AdminCustomerController::class, 'toggleStatus'])->name('customers.toggle-status')->middleware('module:customers');
    Route::resource('customers', AdminCustomerController::class)->middleware('module:customers');

    // Customer Saved Addresses
    Route::post('/customers/{customer}/addresses', [AdminCustomerAddressController::class, 'store'])->name('customers.addresses.store')->middleware('module:customers');
    Route::put('/customers/{customer}/addresses/{address}', [AdminCustomerAddressController::class, 'update'])->name('customers.addresses.update')->middleware('module:customers');
    Route::delete('/customers/{customer}/addresses/{address}', [AdminCustomerAddressController::class, 'destroy'])->name('customers.addresses.destroy')->middleware('module:customers');
    Route::post('/customers/{customer}/addresses/{address}/set-default/{type}', [AdminCustomerAddressController::class, 'setDefault'])->name('customers.addresses.set-default')->middleware('module:customers');

    // Products
    Route::get('products/{product}/duplicate', [AdminProductController::class, 'duplicate'])->middleware('module:products')->name('products.duplicate');
    Route::resource('products', AdminProductController::class)->middleware('module:products');

    // Categories
    Route::resource('categories', AdminCategoryController::class)->middleware('module:products');

    // Subcategories (Separate CRUD)
    Route::resource('subcategories', AdminSubcategoryController::class)->middleware('module:products');

    // Brands & Units
    Route::resource('brands', AdminBrandController::class)->middleware('module:products');
    Route::resource('units', AdminUnitController::class)->middleware('module:products');

    // Slides
    Route::resource('slides', AdminSlideController::class)->middleware('module:content');

    // FAQs
    Route::resource('faqs', AdminFaqController::class)->middleware('module:content');

    // Orders
    Route::resource('orders', AdminOrderController::class)->only(['index', 'create', 'store', 'show', 'update', 'destroy'])->middleware('module:orders');

    // Settings
    Route::get('/settings', [AdminSettingController::class, 'index'])->middleware('module:settings')->name('settings.index');
    Route::put('/settings', [AdminSettingController::class, 'update'])->middleware('module:settings')->name('settings.update');

    // Payments
    Route::get('/payments', [AdminPaymentController::class, 'index'])->middleware('module:payments')->name('payments.index');
    Route::put('/payments/{id}', [AdminPaymentController::class, 'update'])->middleware('module:payments')->name('payments.update');
    Route::delete('/payments/{id}', [AdminPaymentController::class, 'destroy'])->middleware('module:payments')->name('payments.destroy');
    Route::resource('payment-methods', AdminPaymentMethodController::class)->except(['show'])->middleware('module:payments');

    // Shipping Methods
    Route::resource('shipping-methods', AdminShippingMethodController::class)->except(['show'])->middleware('module:settings');

    // Reports
    Route::get('/reports/sales', [AdminReportController::class, 'sales'])->middleware('module:reports')->name('reports.sales');
    Route::get('/reports/products', [AdminReportController::class, 'products'])->middleware('module:reports')->name('reports.products');

    // Page Content Management
    Route::resource('page-content', PageContentController::class)->middleware('module:content');
});

require __DIR__.'/auth.php';
