<?php
function gp_theme_setting(PDO $pdo, string $key, string $default = ''): string
{
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = :k LIMIT 1");
    $stmt->execute([':k' => $key]);
    $v = $stmt->fetchColumn();
    return $v !== false ? (string)$v : $default;
}

function gp_theme_config(PDO $pdo): array
{
    return [
        'app_name' => gp_theme_setting($pdo, 'brand_app_name', 'GoPools CRM'),
        'logo' => gp_theme_setting($pdo, 'brand_logo', ''),
        'favicon' => gp_theme_setting($pdo, 'brand_favicon', ''),
        'mode' => gp_theme_setting($pdo, 'theme_mode', 'light'),
        'primary' => gp_theme_setting($pdo, 'theme_primary', '#2563eb'),
        'secondary' => gp_theme_setting($pdo, 'theme_secondary', '#0f172a'),
        'glass' => gp_theme_setting($pdo, 'theme_glass', '1') === '1',
        'background_type' => gp_theme_setting($pdo, 'background_type', 'gradient'),
        'background_color' => gp_theme_setting($pdo, 'background_color', '#f8fafc'),
        'background_image' => gp_theme_setting($pdo, 'background_image', ''),
        'background_gradient' => gp_theme_setting($pdo, 'background_gradient', 'linear-gradient(135deg,#f8fafc 0%,#e2e8f0 100%)'),
        'background_overlay_opacity' => gp_theme_setting($pdo, 'background_overlay_opacity', '0.10'),
    ];
}

function gp_theme_background_css(array $theme): string
{
    $type = $theme['background_type'] ?? 'gradient';
    if ($type === 'image' && !empty($theme['background_image'])) {
        return "url('" . addslashes((string)$theme['background_image']) . "') center/cover no-repeat fixed";
    }
    if ($type === 'color') {
        return (string)$theme['background_color'];
    }
    return (string)$theme['background_gradient'];
}
