<?php
declare(strict_types=1);

/**
 * CHUTOGEL RTP — front controller.
 *
 * Micro-framework, no SQL: every dataset lives in a JSON file under storage/data.
 * All state writes go through JsonStore, which locks and writes atomically.
 */

define('APP_ROOT', __DIR__);
define('STORAGE', APP_ROOT . '/storage');

require APP_ROOT . '/app/config.php';

spl_autoload_register(static function (string $class): void {
    $prefix = 'App\\';
    if (strncmp($class, $prefix, strlen($prefix)) !== 0) {
        return;
    }
    $rel = str_replace('\\', '/', substr($class, strlen($prefix)));
    $file = APP_ROOT . '/app/' . $rel . '.php';
    if (is_file($file)) {
        require $file;
    }
});

$app = new App\Core\Application(APP_ROOT, STORAGE);

$router = $app->router();
$router->get('/', 'HomeController@index');
$router->get('/provider/{slug}', 'ProviderController@show');
$router->get('/api/providers', 'ApiController@providers');
$router->get('/api/provider/{slug}', 'ApiController@provider');
$router->get('/api/live', 'ApiController@live');
$router->get('/api/refresh', 'ApiController@refresh');
$router->get('/robots.txt', 'SitemapController@robots');
$router->get('/sitemap.xml', 'SitemapController@sitemap');

$app->run();