<?php

if (!function_exists('theme')) {
    /**
     * Get theme configuration or specific theme property
     *
     * @param string|null $key Optional key to get specific theme property
     * @param mixed $default Default value if key is not found
     * @return mixed
     */
    function theme($key = null, $default = null)
    {
        $activeTheme = config('themes.active', 'timebank_cc');
        $themes = config('themes.themes', []);
        
        if (!isset($themes[$activeTheme])) {
            $activeTheme = 'timebank_cc'; // fallback to default
        }
        
        $themeConfig = $themes[$activeTheme] ?? [];
        
        if ($key === null) {
            return array_merge(['id' => $activeTheme], $themeConfig);
        }
        
        return data_get($themeConfig, $key, $default);
    }
}

if (!function_exists('theme_name')) {
    /**
     * Get the current theme name
     *
     * @return string
     */
    function theme_name()
    {
        return theme('name', 'Timebank.cc');
    }
}

if (!function_exists('theme_id')) {
    /**
     * Get the current theme ID/key
     *
     * @return string
     */
    function theme_id()
    {
        return config('themes.active', 'timebank_cc');
    }
}

if (!function_exists('theme_color')) {
    /**
     * Get a theme color value
     *
     * @param string $colorKey Color key (e.g., 'primary.500', 'accent', 'text.primary')
     * @param string|null $default Default color value
     * @return string
     */
    function theme_color($colorKey, $default = null)
    {
        return theme("colors.{$colorKey}", $default);
    }
}

if (!function_exists('theme_font')) {
    /**
     * Get a theme typography value
     *
     * @param string $fontKey Font key (e.g., 'font_family_body', 'font_size_base')
     * @param string|null $default Default font value
     * @return string
     */
    function theme_font($fontKey, $default = null)
    {
        return theme("typography.{$fontKey}", $default);
    }
}

if (!function_exists('is_theme')) {
    /**
     * Check if the current theme matches the given theme ID
     *
     * @param string $themeId Theme ID to check against
     * @return bool
     */
    function is_theme($themeId)
    {
        return theme_id() === $themeId;
    }
}

if (!function_exists('theme_logo')) {
    /**
     * Get a theme logo path or view name
     *
     * @param string $type Logo type: 'svg_inline' or 'email_logo'
     * @param string|null $default Default value if logo not configured
     * @return string
     */
    function theme_logo($type = 'svg_inline', $default = null)
    {
        return theme("logos.{$type}", $default);
    }
}

if (!function_exists('theme_css_vars')) {
    /**
     * Generate CSS custom properties for the current theme
     *
     * @return string CSS custom properties as inline styles
     */
    function theme_css_vars()
    {
        $themeConfig = theme();
        $colors = $themeConfig['colors'] ?? [];
        $typography = $themeConfig['typography'] ?? [];
        
        $cssVars = [];
        
        // Process color variables
        foreach ($colors as $colorGroup => $colorValue) {
            if (is_array($colorValue)) {
                foreach ($colorValue as $shade => $hex) {
                    $rgbValue = hexToRgb($hex);
                    $cssVars["--color-{$colorGroup}-{$shade}"] = $rgbValue;
                }
            } else {
                $rgbValue = hexToRgb($colorValue);
                $cssVars["--color-{$colorGroup}"] = $rgbValue;
            }
        }
        
        // Process typography variables
        foreach ($typography as $typographyKey => $typographyValue) {
            if (is_array($typographyValue)) {
                // Handle nested arrays (heading_sizes, font_sizes, font_weights)
                foreach ($typographyValue as $subKey => $subValue) {
                    $cssVars["--{$typographyKey}-{$subKey}"] = $subValue;
                }
            } else {
                $cssVars["--{$typographyKey}"] = $typographyValue;
            }
        }
        
        // Convert to CSS string
        $cssString = '';
        foreach ($cssVars as $property => $value) {
            $cssString .= "{$property}: {$value}; ";
        }
        
        return $cssString;
    }
}

if (!function_exists('hexToRgb')) {
    /**
     * Convert hex color to RGB values (space-separated for CSS custom properties)
     *
     * @param string $hex Hex color value
     * @return string RGB values separated by spaces
     */
    function hexToRgb($hex)
    {
        $hex = ltrim($hex, '#');
        
        if (strlen($hex) === 3) {
            $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
        }
        
        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));
        
        return "{$r} {$g} {$b}";
    }
}