<?php
// ─── Domain Settings ─────────────────────────────────────────────────────────
// Registrar API integration itself now lives in core/registrars.php (pluggable
// drivers, admin-managed via h_registrar) — this file just holds the generic
// settings helper and the free-domain pricing rule.
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
    header('HTTP/1.0 403 Forbidden', true, 403);
    exit;
}

if (!function_exists('get_setting')) {
    function get_setting($name, $default = '') {
        global $conn;
        $nameSafe = mysqli_real_escape_string($conn, $name);
        $row = mysqli_fetch_assoc(mysqli_query($conn, "SELECT value FROM h_settings WHERE name='$nameSafe'"));
        return $row['value'] ?? $default;
    }
}

if (!function_exists('save_setting')) {
    function save_setting($name, $value) {
        global $conn;
        $nameSafe = mysqli_real_escape_string($conn, $name);
        $valueSafe = mysqli_real_escape_string($conn, $value);
        mysqli_query($conn, "INSERT INTO h_settings (name, value) VALUES ('$nameSafe','$valueSafe') ON DUPLICATE KEY UPDATE value='$valueSafe'");
    }
}

// ─── Free-domain pricing rule ───────────────────────────────────────────────
// A domain counts as "free" purely based on its shopped registrar cost
// (see core/registrars.php@shop_domain) vs. an admin-adjustable threshold
// (default $2/yr) — no manual per-TLD flag, no hardcoded cutoff.
function free_domain_threshold() {
    return (float) get_setting('free_domain_threshold', '2.00');
}
function tld_is_free($price) {
    return (float)$price <= free_domain_threshold();
}
