<?php

declare(strict_types=1);

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

require dirname(__DIR__) . '/touch.polakis.co.ke/backend/src/Core/App.php';

use App\Controllers\AdminController;
use App\Controllers\ApiController;
use App\Core\App;
use App\Core\Request;
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();

// --- 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/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/pages/{slug}', fn($req, $params) => $api->page($req, $params));
$router->post('/api/contact', fn($req) => $api->contact($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->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);
