<?php

declare(strict_types=1);

/**
 * Touch Therapies Antigua — front controller.
 * Serves the REST API (/api/*) and the CMS admin (/admin/*).
 */

require dirname(__DIR__) . '/src/Core/App.php';

use App\Controllers\AdminController;
use App\Controllers\ApiController;
use App\Controllers\PaymentController;
use App\Core\App;
use App\Core\Request;
use App\Core\Response;
use App\Core\Router;

App::boot();

// PHP built-in dev server: let it serve real files (assets, uploads) directly.
if (PHP_SAPI === 'cli-server') {
    $file = __DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if (is_file($file)) {
        return false;
    }
    $upload = dirname(__DIR__, 2) . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if (str_starts_with($_SERVER['REQUEST_URI'], '/uploads/') && is_file($upload)) {
        header('Content-Type: ' . (mime_content_type($upload) ?: 'application/octet-stream'));
        readfile($upload);
        exit;
    }
}

$request = new Request();
$router = new Router();
$api = new ApiController();
$admin = new AdminController();
$pay = new PaymentController();

// Landing on the install folder itself (e.g. /backend/) → CMS.
$router->get('/', fn() => Response::redirect('/admin'));

// --- Public REST API --------------------------------------------------------
$router->get('/api/settings', fn() => $api->settings());
$router->get('/api/homepage', fn() => $api->homepage());
$router->get('/api/treatments', fn() => $api->treatments());
$router->get('/api/rooms', fn() => $api->rooms());
$router->get('/api/availability', fn($req) => $api->availability($req));
$router->get('/api/packages', fn() => $api->packages());
$router->get('/api/gallery', fn() => $api->gallery());
$router->get('/api/testimonials', fn() => $api->testimonials());
$router->get('/api/team', fn() => $api->team());
$router->get('/api/blog', fn() => $api->blog());
$router->get('/api/faqs', fn() => $api->faqs());
$router->get('/api/guesthouse', fn() => $api->guesthouse());
$router->get('/api/guesthouse/units', fn() => $api->guesthouseUnits());
$router->get('/api/guesthouse/units/{id}/availability', fn($req, $p) => $api->guesthouseUnitAvailability($req, $p));
$router->get('/api/pages', fn() => $api->pages());
$router->get('/api/pages/{slug}', fn($req, $params) => $api->page($req, $params));
$router->post('/api/contact', fn($req) => $api->contact($req));
$router->post('/api/chat', fn($req) => $api->chat($req));

// --- Booking deposits (PayPal + WiPay) ---------------------------------------
$router->post('/api/bookings', fn($req) => $pay->book($req));
$router->get('/api/bookings/{reference}', fn($req, $p) => $pay->status($req, $p));
$router->post('/api/payments/paypal/order', fn($req) => $pay->paypalOrder($req));
$router->post('/api/payments/paypal/capture', fn($req) => $pay->paypalCapture($req));
$router->post('/api/payments/wipay/checkout', fn($req) => $pay->wipayCheckout($req));
$router->get('/api/payments/wipay/response', fn($req) => $pay->wipayResponse($req));

// --- CMS admin ---------------------------------------------------------------
$router->get('/admin/login', fn() => $admin->loginForm());
$router->post('/admin/login', fn($req) => $admin->login($req));
$router->post('/admin/logout', fn($req) => $admin->logout($req));
$router->get('/admin', fn() => $admin->dashboard());
$router->get('/admin/settings', fn($req) => $admin->settings($req));
$router->post('/admin/settings/save', fn($req) => $admin->saveSettings($req));
$router->post('/admin/ai/rebuild', fn($req) => $admin->aiRebuild($req));
$router->get('/admin/contacts', fn($req) => $admin->contacts($req));
$router->post('/admin/contacts/{id}/toggle', fn($req, $p) => $admin->contactHandled($req, $p));
$router->post('/admin/media/upload', fn($req) => $admin->upload($req));

// Generic CRUD (declared last: {module} would otherwise shadow the routes above).
$router->get('/admin/{module}', fn($req, $p) => $admin->index($req, $p));
$router->get('/admin/{module}/create', fn($req, $p) => $admin->create($req, $p));
$router->post('/admin/{module}/save', fn($req, $p) => $admin->save($req, $p));
$router->get('/admin/{module}/{id}/edit', fn($req, $p) => $admin->edit($req, $p));
$router->post('/admin/{module}/{id}/save', fn($req, $p) => $admin->save($req, $p));
$router->post('/admin/{module}/{id}/delete', fn($req, $p) => $admin->destroy($req, $p));

$router->dispatch($request);
