<?php
/**
 * webview.php — Generic in-app WebView configuration endpoint
 *
 * Called by the app at: {panel_url}api/webview.php
 * (see ca.krankhaus.tivi.config.AppConfig.webviewConfigUrl())
 *
 * Returns JSON configuration for a remotely-controlled, generic
 * in-app WebView, mirroring the same shape used by api/sports.php.
 */

require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: application/json; charset=UTF-8');

require_valid_signature();

logRequest('api/webview.php');

$config = $db->select('webview_config', '*', 'id = :id', '', [':id' => 1]);

if (empty($config)) {
    echo json_encode([
        'success' => true,
        'enabled' => false,
        'title' => 'Web',
        'url' => '',
        'javascript' => true,
        'allow_external' => false,
        'user_agent' => '',
        'maintenance_message' => '',
        'cache_mode' => 'default',
    ], JSON_UNESCAPED_SLASHES);
    exit;
}

$c = $config[0];

echo json_encode([
    'success' => true,
    'enabled' => ((int) ($c['enabled'] ?? 0)) === 1,
    'title' => $c['title'] ?? 'Web',
    'url' => $c['url'] ?? '',
    'javascript' => ((int) ($c['javascript'] ?? 1)) === 1,
    'allow_external' => ((int) ($c['allow_external'] ?? 0)) === 1,
    'user_agent' => $c['user_agent'] ?? '',
    'maintenance_message' => $c['maintenance_message'] ?? '',
    'cache_mode' => $c['cache_mode'] ?? 'default',
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
