#!/usr/bin/env php81
<?php
/**
 * Restore url-shortener prod config after rsync/submodule deploy (config.php is gitignored).
 *
 * On BE (as user that can read crashes config.php):
 *   php81 examples/crash_reporter/backend/scripts/ensure_url_shortener_prod_config.php
 *
 * - Writes backend/url-shortener/config/config.php (via apps/s symlink when present)
 * - Ensures crashes config.php has url_shortener.enabled + db block
 */
declare(strict_types=1);

$repoRoot = realpath(dirname(__DIR__, 3)) ?: dirname(__DIR__, 3);
$crashConfigPath = $repoRoot . '/examples/crash_reporter/backend/config/config.php';
$urlConfigDir = $repoRoot . '/backend/url-shortener/config';
$urlConfigPath = $urlConfigDir . '/config.php';

$sLink = '/var/www/localhost/htdocs/apps/s';
if (is_link($sLink)) {
    $resolved = realpath($sLink);
    if ($resolved !== false) {
        $urlConfigDir = $resolved . '/config';
        $urlConfigPath = $urlConfigDir . '/config.php';
    }
}

function fail(string $msg): void {
    fwrite(STDERR, "ensure_url_shortener_prod_config: $msg\n");
    exit(1);
}

if (!is_readable($crashConfigPath)) {
    fail("missing crashes config: $crashConfigPath");
}

/** @var array<string,mixed> $crash */
$crash = require $crashConfigPath;
$m = $crash['db']['mysql'] ?? null;
if (!is_array($m) || ($m['password'] ?? '') === '') {
    fail('crashes db.mysql.password not set in config.php');
}

$user = (string) ($m['username'] ?? 'androidcast');
$pass = (string) $m['password'];
$socket = (string) ($m['socket'] ?? '/run/mysqld/mysqld.sock');

try {
    $pdo = new PDO(
        "mysql:unix_socket={$socket};dbname=url_shortener;charset=utf8mb4",
        $user,
        $pass,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    $pdo->query('SELECT 1');
} catch (Throwable $e) {
    fail('url_shortener DB ping failed: ' . $e->getMessage());
}

$urlBlock = [
    'db' => [
        'driver' => 'mysql',
        'mysql' => [
            'host' => '127.0.0.1',
            'port' => 3306,
            'socket' => $socket,
            'database' => 'url_shortener',
            'username' => $user,
            'password' => $pass,
            'charset' => 'utf8mb4',
        ],
    ],
    'public_base' => 'https://s.f0xx.org',
];

if (!is_dir($urlConfigDir)) {
    fail("url-shortener config dir missing: $urlConfigDir");
}

file_put_contents($urlConfigPath, "<?php\nreturn " . var_export($urlBlock, true) . ";\n");
chmod($urlConfigPath, 0644);

$crashUrl = [
    'enabled' => true,
    'public_base' => 'https://s.f0xx.org',
    'db' => [
        'mysql' => [
            'host' => '127.0.0.1',
            'port' => 3306,
            'socket' => $socket,
            'database' => 'url_shortener',
            'username' => $user,
            'password' => $pass,
            'charset' => 'utf8mb4',
        ],
    ],
];

$crash['url_shortener'] = $crashUrl;
file_put_contents($crashConfigPath, "<?php\nreturn " . var_export($crash, true) . ";\n");

echo "ok: $urlConfigPath\n";
echo "ok: crashes url_shortener.enabled=true\n";
