<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\AccountController;
use App\Http\Controllers\BulkInquiryController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminCategoryController;
use App\Http\Controllers\Admin\AdminProductController;
use App\Http\Controllers\Admin\AdminOrderController;
use App\Http\Controllers\Admin\AdminBulkInquiryController;
use App\Http\Controllers\Admin\AdminContactController;
use App\Http\Controllers\Admin\AdminDeliveryController;
use App\Http\Controllers\Admin\AdminSettingsController;
use App\Http\Controllers\Admin\AdminBannerController;
use App\Http\Controllers\Admin\AdminBrandController;

/*
|--------------------------------------------------------------------------
| Frontend Routes
|--------------------------------------------------------------------------
*/
Route::get('/mars-setup-2026', function () {
    if (request('key') !== 'mars2026setup') abort(404);
    $output = '<h2>Mars Setup</h2><pre>';

    // Storage symlink
    $target = storage_path('app/public');
    $link = public_path('storage');
    $output .= "Target: $target\nLink: $link\n\n";

    if (is_link($link)) {
        $output .= "Existing symlink found, removing...\n";
        unlink($link);
    } elseif (is_dir($link)) {
        $output .= "Directory exists at link path, removing...\n";
        exec("rm -rf " . escapeshellarg($link));
    }

    if (symlink($target, $link)) {
        $output .= "Symlink created successfully!\n";
    } else {
        exec("ln -sf " . escapeshellarg($target) . " " . escapeshellarg($link));
        $output .= "Symlink created via shell.\n";
    }

    // Run artisan commands
    $commands = ['storage:link', 'migrate --force', 'db:seed --force', 'config:clear', 'cache:clear', 'view:clear'];
    foreach ($commands as $cmd) {
        $output .= "\n> php artisan $cmd\n";
        \Artisan::call($cmd);
        $output .= \Artisan::output();
    }

    // Verify
    $output .= "\n--- Verification ---\n";
    $output .= "Symlink exists: " . (is_link($link) ? 'YES' : 'NO') . "\n";
    if (is_dir($link . '/products')) {
        $images = glob($link . '/products/*.jpg');
        $output .= "Product images found: " . count($images) . "\n";
    } else {
        $output .= "Products dir not accessible via symlink\n";
    }

    $output .= "</pre>";
    return $output;
});

Route::get('/', [HomeController::class, 'index']);

// Products
Route::get('/products', [ProductController::class, 'index']);
Route::get('/products/{slug}', [ProductController::class, 'show']);
Route::get('/category/{slug}', [ProductController::class, 'category']);

// Cart
Route::get('/cart', [CartController::class, 'index']);
Route::post('/cart/add', [CartController::class, 'add']);
Route::post('/cart/update', [CartController::class, 'update']);
Route::post('/cart/remove', [CartController::class, 'remove']);

// Checkout
Route::get('/checkout', [CheckoutController::class, 'index']);
Route::post('/checkout', [CheckoutController::class, 'store']);
Route::get('/order-confirmation/{order}', [CheckoutController::class, 'confirmation']);
Route::post('/order/{order}/upload-receipt', [CheckoutController::class, 'uploadReceipt']);

// Contact
Route::get('/contact', [ContactController::class, 'index']);
Route::post('/contact', [ContactController::class, 'store']);

// Bulk Inquiry
Route::get('/bulk-inquiry', [BulkInquiryController::class, 'index']);
Route::post('/bulk-inquiry', [BulkInquiryController::class, 'store']);
Route::post('/product-inquiry', [BulkInquiryController::class, 'productInquiry']);

// Account (auth required)
Route::middleware('auth')->group(function () {
    Route::get('/account', [AccountController::class, 'dashboard']);
    Route::get('/account/orders', [AccountController::class, 'orders']);
    Route::get('/account/orders/{order}', [AccountController::class, 'orderDetail']);
    Route::get('/account/addresses', [AccountController::class, 'addresses']);
    Route::post('/account/addresses', [AccountController::class, 'storeAddress']);
    Route::put('/account/addresses/{address}', [AccountController::class, 'updateAddress']);
    Route::delete('/account/addresses/{address}', [AccountController::class, 'deleteAddress']);
    Route::get('/account/profile', [AccountController::class, 'profile']);
    Route::put('/account/profile', [AccountController::class, 'updateProfile']);
});

/*
|--------------------------------------------------------------------------
| Auth Routes
|--------------------------------------------------------------------------
*/
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterController::class, 'register']);

/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->middleware(['auth', 'admin'])->name('admin.')->group(function () {

    // Dashboard
    Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');

    // Categories
    Route::resource('categories', AdminCategoryController::class);

    // Products
    Route::resource('products', AdminProductController::class);

    // Orders
    Route::get('orders', [AdminOrderController::class, 'index'])->name('orders.index');
    Route::get('orders/{order}', [AdminOrderController::class, 'show'])->name('orders.show');
    Route::patch('orders/{order}/status', [AdminOrderController::class, 'updateStatus'])->name('orders.updateStatus');

    // Bulk Inquiries
    Route::get('inquiries', [AdminBulkInquiryController::class, 'index'])->name('inquiries.index');
    Route::get('inquiries/{inquiry}', [AdminBulkInquiryController::class, 'show'])->name('inquiries.show');
    Route::patch('inquiries/{inquiry}/status', [AdminBulkInquiryController::class, 'updateStatus'])->name('inquiries.updateStatus');

    // Contact Messages
    Route::get('contacts', [AdminContactController::class, 'index'])->name('contacts.index');
    Route::get('contacts/{contact}', [AdminContactController::class, 'show'])->name('contacts.show');

    // Delivery Settings
    Route::get('delivery', [AdminDeliveryController::class, 'edit'])->name('delivery.edit');
    Route::put('delivery', [AdminDeliveryController::class, 'update'])->name('delivery.update');

    // Site Settings
    Route::get('settings', [AdminSettingsController::class, 'edit'])->name('settings.edit');
    Route::put('settings', [AdminSettingsController::class, 'update'])->name('settings.update');

    // Banners
    Route::resource('banners', AdminBannerController::class);

    // Brands
    Route::resource('brands', AdminBrandController::class);
});
