<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require_login();

$domainId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($domainId <= 0) {
    redirect('/admin/domain/list.php?check_status=missing');
}

try {
    ensure_domains_table_has_site_root();
    $domain = find_domain_by_id($domainId);
    if (!$domain) {
        redirect('/admin/domain/list.php?check_status=missing');
    }
    $result = check_domain_site_root_status($domain);
    $status = !empty($result['ok']) ? 'success' : 'error';
    redirect('/admin/domain/list.php?check_status=' . urlencode($status) . '&checked_domain=' . urlencode(isset($result['domain']) ? (string)$result['domain'] : '') . '&check_message=' . urlencode(isset($result['message']) ? (string)$result['message'] : '') . '&checked_root=' . urlencode(isset($result['site_root']) ? (string)$result['site_root'] : ''));
} catch (Exception $e) {
    redirect('/admin/domain/list.php?check_status=error&check_message=' . urlencode($e->getMessage()));
}
