<?php

use App\Http\Controllers\AboutController;
use App\Http\Controllers\AccountController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\Portal\BookingFormController;
use App\Http\Controllers\Portal\BookingsController;
use App\Http\Controllers\ServicesController;
use App\Http\Middleware\EnsureUserIsApproved;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

// Public pages
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/about', [AboutController::class, 'index'])->name('about');
Route::get('/services', [ServicesController::class, 'index'])->name('services');
Route::get('/news', [NewsController::class, 'index'])->name('news.index');
Route::get('/news/{news:slug}', [NewsController::class, 'show'])->name('news.show');
Route::get('/contact', [ContactController::class, 'index'])->name('contact');
Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');

// Auth routes (guests only)
Route::middleware('guest')->group(function () {
    Route::get('/register', [RegisteredUserController::class, 'create'])->name('register');
    Route::post('/register', [RegisteredUserController::class, 'store']);

    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store']);

    Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])->name('password.email');

    Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [NewPasswordController::class, 'store'])->name('password.store');
});

// Logout
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
    ->middleware('auth')
    ->name('logout');

// Account — any authenticated user
Route::middleware('auth')->group(function () {
    Route::get('/account', [AccountController::class, 'index'])->name('account.index');
    Route::get('/portal/pending', [AccountController::class, 'pending'])->name('portal.pending');
    Route::get('/portal/bookings', [BookingsController::class, 'index'])->name('portal.bookings');
    Route::put('/account/profile', [AccountController::class, 'updateProfile'])->name('account.profile.update');
    Route::put('/account/password', [AccountController::class, 'updatePassword'])->name('account.password.update');
});

// ── Deploy helper ────────────────────────────────────────────────────────────
// https://elemanclub.com/deploy?key=YOUR_DEPLOY_KEY
// Set DEPLOY_KEY=random_secret in .env — keep it private.
Route::get('/deploy/clear-cache', function (\Illuminate\Http\Request $request) {
    $key = config('app.deploy_key');
    if ($key && $request->query('key') !== $key) {
        abort(403, 'Invalid deploy key.');
    }

    $output = [];
    foreach (['config:clear', 'cache:clear', 'view:clear', 'route:clear'] as $cmd) {
        Artisan::call($cmd);
        $output[$cmd] = trim(Artisan::output()) ?: 'OK';
    }

    return response()->json(['status' => 'done', 'results' => $output]);
})->name('deploy.clear-cache');

Route::get('/deploy/storage-link', function (\Illuminate\Http\Request $request) {
    $key = config('app.deploy_key');
    if ($key && $request->query('key') !== $key) {
        abort(403, 'Invalid deploy key.');
    }

    Artisan::call('storage:link');

    return response()->json(['status' => 'done', 'result' => trim(Artisan::output()) ?: 'OK']);
})->name('deploy.storage-link');

// Booking forms — approved users only
Route::middleware(['auth', EnsureUserIsApproved::class])->group(function () {
    Route::get('/book/{facility}', [BookingFormController::class, 'show'])->name('book');
    Route::post('/book/{facility}', [BookingFormController::class, 'store'])->name('book.store');
});
