<?php
declare(strict_types=1);
/**
 * Application: tstapp
 * File: index.php
 * Version: 1.0.0
 * Description: Primary application entry point / controller.
 * 
 * TEST CURL:
 *   curl -k -s "https://rickweb.test:8443/tstapp/index.php" | json_pp
 * TEST CLI:
 *   php index.php
 */

require_once __DIR__ . APP_NAME. '/setup.php';

try {
    $config = require __DIR__ . APP_NAME.'/config.php';
    $data = [
        'app' => APP_NAME,
        'status' => 'online',
        'root' => APP_ROOT,
        'config' => $config
    ];

    if (isset($_GET['page']) && $_GET['page'] === 'status') {
        require __DIR__ . '/pages/status.php';
        exit;
    }

		$viewfile="/".APP_NAME."/view.php";
		$viewfile="";
    if (file_exists(__DIR__ . $viewfile)) {
        require __DIR__ . "$viewfile";
    } else {
        header('Content-Type: application/json');
        echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    }

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => true, 'message' => $e->getMessage()]);
}
