<?php

use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\ContactController as AdminContactController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\OrderController as AdminOrderController;
use App\Http\Controllers\Admin\QuoteController as AdminQuoteController;
use App\Http\Controllers\Admin\EmployeeController as AdminEmployeeController;
use App\Http\Controllers\Admin\ServiceController as AdminServiceController;
use App\Http\Controllers\Admin\SettingsController as AdminSettingsController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\CustomQuoteController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Route;

// Public routes
Route::get('/', fn() => view('home'))->name('home');
Route::get('/about', fn() => view('about'))->name('about');
Route::get('/faq', fn() => view('faq'))->name('faq');
Route::get('/privacy-policy', fn() => view('privacy'))->name('privacy');
Route::get('/terms-and-conditions', fn() => view('terms'))->name('terms');

Route::get('/services', [ServiceController::class, 'index'])->name('services.index');
Route::get('/services/{slug}', [ServiceController::class, 'show'])->name('services.show');

Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
Route::post('/cart/add/{slug}', [CartController::class, 'add'])->name('cart.add');
Route::delete('/cart/remove/{slug}', [CartController::class, 'remove'])->name('cart.remove');
Route::post('/cart/clear', [CartController::class, 'clear'])->name('cart.clear');

Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::post('/checkout', [CheckoutController::class, 'process'])->name('checkout.process');
Route::get('/checkout/success/{order}', [CheckoutController::class, 'success'])->name('checkout.success');
Route::get('/checkout/crypto/{order}', [CheckoutController::class, 'cryptoInstructions'])->name('checkout.crypto');

Route::get('/contact', [ContactController::class, 'index'])->name('contact.index');
Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');

Route::get('/custom-quote', [CustomQuoteController::class, 'index'])->name('custom-quote.index');
Route::post('/custom-quote', [CustomQuoteController::class, 'store'])->name('custom-quote.store');

// Card2Crypto webhook — CSRF exempt via bootstrap/app.php
Route::post('/webhooks/card2crypto', [WebhookController::class, 'card2crypto'])
    ->name('webhooks.card2crypto');

// Admin routes — secret prefix, not linked publicly
$adminPrefix = config('admin.prefix', 'tm-staff-9x2k');

Route::prefix($adminPrefix)->name('admin.')->group(function () {
    // Login (no auth required)
    Route::get('/login', [AdminAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AdminAuthController::class, 'login'])->name('login.post');

    // Protected admin area
    Route::middleware(['auth', \App\Http\Middleware\EnsureIsAdmin::class, \App\Http\Middleware\RestrictAdminByIp::class])
        ->group(function () {
            Route::post('/logout', [AdminAuthController::class, 'logout'])->name('logout');
            Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
            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.status');
            Route::patch('/orders/{order}/notes', [AdminOrderController::class, 'updateNotes'])->name('orders.notes');
            Route::patch('/orders/{order}/target-info', [AdminOrderController::class, 'updateTargetInfo'])->name('orders.target-info');
            Route::patch('/orders/{order}/assign', [AdminOrderController::class, 'assignEmployees'])->name('orders.assign');

            Route::resource('/employees', AdminEmployeeController::class)->except(['show']);

            Route::get('/settings', [AdminSettingsController::class, 'index'])->name('settings.index');
            Route::post('/settings/smtp', [AdminSettingsController::class, 'updateSmtp'])->name('settings.smtp');
            Route::post('/settings/payment', [AdminSettingsController::class, 'updatePayment'])->name('settings.payment');
            Route::post('/settings/general', [AdminSettingsController::class, 'updateGeneral'])->name('settings.general');
            Route::get('/contacts', [AdminContactController::class, 'index'])->name('contacts.index');
            Route::get('/contacts/{contact}', [AdminContactController::class, 'show'])->name('contacts.show');
            Route::get('/quotes', [AdminQuoteController::class, 'index'])->name('quotes.index');
            Route::get('/quotes/{quote}', [AdminQuoteController::class, 'show'])->name('quotes.show');

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

// TEMPORARY: Database migration URL
// Visit once: https://yourdomain.com/setup-migrate-db/secret123
// Then remove these lines from this file!
Route::get('/setup-migrate-db/{token}', function ($token) {
    if ($token !== 'secret123') abort(404);
    try {
        \Artisan::call('migrate', ['--force' => true]);
        return '✅ Migrations completed!<br><pre>' . \Artisan::output() . '</pre>';
    } catch (\Exception $e) {
        return '❌ Error: ' . $e->getMessage();
    }
});

// TEMPORARY: Create default admin user
// Visit: https://yourdomain.com/setup-admin/secret123
Route::get('/setup-admin/{token}', function ($token) {
    if ($token !== 'secret123') abort(404);
    $user = \App\Models\User::create([
        'name' => 'Admin',
        'email' => 'admin@trollmasters.ai',
        'password' => \Illuminate\Support\Facades\Hash::make('admin123'),
        'is_admin' => true,
    ]);
    return '✅ Admin created!<br>Email: admin@trollmasters.ai<br>Password: admin123<br>⚠️ Change password after first login!';
});
