<?php

/**
 * Generates and returns inline CSS for the active author byline box template.
 *
 * To improve performance, each template's CSS is cached separately and only regenerated
 * when Customizer settings are updated.
 *
 * @return string Sanitized inline CSS.
 */


if (! defined('ABSPATH')) {
    exit;
}


function author_kit_generate_byline_inline_css()
{
    $active_byline_template = get_option('author_kit_custom_byline_template', 'byline-default');
    $cache_key              = 'author_kit_byline_inline_css_' . $active_byline_template;

    // Retrieve cached CSS and return if available
    $cached_css = get_transient($cache_key);
    if ($cached_css !== false && ! empty($cached_css)) {
        return wp_kses_post($cached_css);
    }

    $custom_css = '';

    switch ($active_byline_template) {
        case 'byline-default':
            $settings = array(
                'alignment'       => get_theme_mod('author_kit_byline_alignment_default', 'left'),
                'bg_color'        => get_theme_mod('author_kit_byline_bg_color_default', '#FBFBFB'),
                'text_color'      => get_theme_mod('author_kit_byline_text_color_default', '#333'),
                'separator_color' => get_theme_mod('author_kit_byline_separator_color_default', '#333'),
            );

            $dimensions = get_theme_mod('author_kit_byline_dimensions_default', array());

            // Set default width and height if not present
            $width  = isset($dimensions['width']) ? $dimensions['width'] : '100%';
            $height = isset($dimensions['height']) ? $dimensions['height'] : 'auto';

            $byline_font_sizes = get_theme_mod('author_kit_byline_font_size_default', array());

            $byline_font_size_desktop = isset($byline_font_sizes['desktop']) ? $byline_font_sizes['desktop'] : 14;
            $byline_font_size_mobile  = isset($byline_font_sizes['mobile']) ? $byline_font_sizes['mobile'] : 12;

            $custom_css = "
            .author-kit-byline-container {
            width: {$width};
            height: {$height};
            background-color: {$settings['bg_color']};
        }

        .author-kit-main-author-contributors {
            justify-content: {$settings['alignment']};
        }
        .author-kit-byline-container{
            justify-content: {$settings['alignment']};
        }

        /* Text styling */
        .author-kit-main-author-label,
        .author-kit-contributions-label,
        .author-kit-last-updated,
        .author-kit-published-date,
        .author-kit-byline-container a,
        .author-kit-category-label{
            color: {$settings['text_color']};
        }

        .author-kit-category-label,
        .author-kit-category-label > i {
            color: {$settings['text_color']};
        }

       

        /* Separator styling */
        .author-kit-separator,
        .author-kit-byline-container #comment-separator,
        .author-kit-byline-container #last-updated-date-separator,
        .author-kit-byline-container #published-date-separator,
        .author-kit-byline-container #categories-separator {
            color: {$settings['separator_color']};
        }

        /* Desktop font size styling */
        @media (min-width: 767px) {
            .author-kit-main-author-label,
            .author-kit-contributions-label,
            .author-kit-last-updated,
            .author-kit-published-date,
            .author-kit-byline-container a,
            .author-kit-category-label,
            .author-kit-separator {
                font-size: {$byline_font_size_desktop}px;
            }

            .author-kit-byline-container a.post-category {
                font-size: {$byline_font_size_desktop}px;
            }

            .author-kit-comment-link a.comments-section-link {
                font-size: {$byline_font_size_desktop}px;
            }
        }

        /* Mobile font size styling */
        @media (max-width: 767px) {
            .author-kit-main-author-label,
            .author-kit-contributions-label,
            .author-kit-last-updated,
            .author-kit-published-date,
            .author-kit-byline-container a,
            .author-kit-category-label,
            .author-kit-separator {
                font-size: {$byline_font_size_mobile}px;
            }

            .author-kit-byline-container a.post-category {
                font-size: {$byline_font_size_mobile}px;
            }

            .author-kit-comment-link a.comments-section-link {
                font-size: {$byline_font_size_mobile}px;
            }
        }


        ";
            break;
        case 'template-advo':
            $settings = array(
                'alignment'       => get_theme_mod('author_kit_byline_alignment_advo', 'left'),
                'bg_color'        => get_theme_mod('author_kit_byline_bg_color_advo', '#FBFBFB'),
                'text_color'      => get_theme_mod('author_kit_byline_text_color_advo', '#333'),
                'separator_color' => get_theme_mod('author_kit_byline_separator_color_advo', '#333'),
            );

            $dimensions = get_theme_mod('author_kit_byline_dimensions_advo', array());

            // Set default width and height if not present
            $width  = isset($dimensions['width']) ? $dimensions['width'] : '100%';
            $height = isset($dimensions['height']) ? $dimensions['height'] : 'auto';

            $byline_font_sizes = get_theme_mod('author_kit_byline_font_size_advo', array());

            $byline_font_size_desktop = isset($byline_font_sizes['desktop']) ? $byline_font_sizes['desktop'] : 14;
            $byline_font_size_mobile  = isset($byline_font_sizes['mobile']) ? $byline_font_sizes['mobile'] : 12;

            $custom_css = "
            .author-kit-byline-container {
            width: {$width};
            height: {$height};
            background-color: {$settings['bg_color']};
        }

         .author-kit-main-author-contributors {
            justify-content: {$settings['alignment']};
        }
        .author-kit-byline-container{
            justify-content: {$settings['alignment']};
        }

        /* Text styling */
        .author-kit-main-author-label,
        .author-kit-contributions-label,
        .author-kit-last-updated,
        .author-kit-published-date,
        .author-kit-byline-container a,
        .author-kit-category-label{
            color: {$settings['text_color']};
        }

        .author-kit-category-label,
        .author-kit-category-label > i {
            color: {$settings['text_color']};
        }

        /* Separator styling */
        .author-kit-separator,
        .author-kit-byline-container #comment-separator,
        .author-kit-byline-container #last-updated-date-separator,
        .author-kit-byline-container #published-date-separator,
        .author-kit-byline-container #categories-separator {
            color: {$settings['separator_color']};
        }

        /* Desktop font size styling */
        @media (min-width: 767px) {
            .author-kit-main-author-label,
            .author-kit-contributions-label,
            .author-kit-last-updated,
            .author-kit-published-date,
            .author-kit-byline-container a,
            .author-kit-category-label,
            .author-kit-separator {
                font-size: {$byline_font_size_desktop}px;
            }

            .author-kit-byline-container a.post-category {
                font-size: {$byline_font_size_desktop}px;
            }

            .author-kit-comment-link a.comments-section-link {
                font-size: {$byline_font_size_desktop}px;
            }
        }

        /* Mobile font size styling */
        @media (max-width: 767px) {
            .author-kit-main-author-label,
            .author-kit-contributions-label,
            .author-kit-last-updated,
            .author-kit-published-date,
            .author-kit-byline-container a,
            .author-kit-category-label,
            .author-kit-separator {
                font-size: {$byline_font_size_mobile}px;
            }

            .author-kit-byline-container a.post-category {
                font-size: {$byline_font_size_mobile}px;
            }

            .author-kit-comment-link a.comments-section-link {
                font-size: {$byline_font_size_mobile}px;
            }
        }

        

        ";

            break;
        case 'template-elantra':
            $settings = array(
                'alignment'       => get_theme_mod('author_kit_byline_alignment_elantra', 'left'),
                'bg_color'        => get_theme_mod('author_kit_byline_bg_color_elantra', 'inherit'),
                'text_color'      => get_theme_mod('author_kit_byline_text_color_elantra', '#333'),
                'separator_color' => get_theme_mod('author_kit_byline_separator_color_elantra', '#333'),
            );

            $dimensions = get_theme_mod('author_kit_byline_dimensions_elantra', array());

            // Set default width and height if not present
            $width  = isset($dimensions['width']) ? $dimensions['width'] : '100%';
            $height = isset($dimensions['height']) ? $dimensions['height'] : 'auto';

            $byline_font_sizes = get_theme_mod('author_kit_byline_font_size_elantra', array());

            $byline_font_size_desktop = isset($byline_font_sizes['desktop']) ? $byline_font_sizes['desktop'] : 14;
            $byline_font_size_mobile  = isset($byline_font_sizes['mobile']) ? $byline_font_sizes['mobile'] : 12;

            $custom_css = "
                .author-kit-byline-container {
                width: {$width};
                height: {$height};
                background-color: {$settings['bg_color']};
            }

           .author-kit-main-author-contributors {
            justify-content: {$settings['alignment']};
        }
        .author-kit-byline-container{
            justify-content: {$settings['alignment']};
        }

            /* Text styling */
            .author-kit-main-author-label,
            .author-kit-contributions-label,
            .author-kit-last-updated,
            .author-kit-published-date,
            .author-kit-byline-container a,
            .author-kit-category-label{
                color: {$settings['text_color']};
            }
            .author-kit-byline-container a{
             color: {$settings['text_color']};
            }
            .author-kit-category-label,
            .author-kit-category-label > i {
                color: {$settings['text_color']};
            }


            /* Separator styling */
            .author-kit-separator,
            .author-kit-byline-container #comment-separator,
            .author-kit-byline-container #last-updated-date-separator,
            .author-kit-byline-container #published-date-separator,
            .author-kit-byline-container #categories-separator {
                color: {$settings['separator_color']};
            }

            /* Desktop font size styling */
            @media (min-width: 767px) {
                .author-kit-main-author-label,
                .author-kit-contributions-label,
                .author-kit-last-updated,
                .author-kit-published-date,
                .author-kit-byline-container a,
                .author-kit-category-label,
                .author-kit-separator {
                    font-size: {$byline_font_size_desktop}px;
                }

                .author-kit-byline-container a.post-category {
                    font-size: {$byline_font_size_desktop}px;
                }

                .author-kit-comment-link a.comments-section-link {
                    font-size: {$byline_font_size_desktop}px;
                }
            }

            /* Mobile font size styling */
            @media (max-width: 767px) {
                .author-kit-main-author-label,
                .author-kit-contributions-label,
                .author-kit-last-updated,
                .author-kit-published-date,
                .author-kit-byline-container a,
                .author-kit-category-label,
                .author-kit-separator {
                    font-size: {$byline_font_size_mobile}px;
                }

                .author-kit-byline-container a.post-category {
                    font-size: {$byline_font_size_mobile}px;
                }

                .author-kit-comment-link a.comments-section-link {
                    font-size: {$byline_font_size_mobile}px;
                }
            }

          

            ";

            break;
        default:
            return '';
    }

    // Store generated CSS in cache for 1 month
    set_transient($cache_key, $custom_css, MONTH_IN_SECONDS);

    return $custom_css;
}

/**
 * Enqueues necessary stylesheets for the byline template based on the active template.
 *
 * Ensures styles are only loaded on single posts and pages, but not on archives.
 *
 * @param string $template_name The active byline template name.
 */
function author_kit_enqueue_byline_styles($template_name)
{
    if (is_archive() || ! (is_single() || is_page())) {
        return;
    }

    // Define the base plugin URL for styles
    $base_url_css = AUTHOR_KIT_PLUGIN_URL . 'public/templates/custom-bylines/asset/css/';
    $handle       = "author-kit-$template_name";

    // Enqueue styles based on template name
    switch ($template_name) {
        case 'byline-default':
            wp_enqueue_style($handle, $base_url_css . 'byline-template.css', array(), AUTHOR_KIT_VERSION, 'all');
            if (is_rtl()) {
                wp_enqueue_style($handle . '-rtl', $base_url_css . 'byline-template-rtl.css', array(), AUTHOR_KIT_VERSION, 'all');
            }
            break;

        case 'template-elantra':
            wp_enqueue_style($handle, $base_url_css . 'byline-template.css', array(), AUTHOR_KIT_VERSION, 'all');
            if (is_rtl()) {
                wp_enqueue_style($handle . '-rtl', $base_url_css . 'byline-template-rtl.css', array(), AUTHOR_KIT_VERSION, 'all');
            }
            break;

        case 'template-advo':
            wp_enqueue_style($handle, $base_url_css . 'byline-template.css', array(), AUTHOR_KIT_VERSION, 'all');
            if (is_rtl()) {
                wp_enqueue_style($handle . '-rtl', $base_url_css . 'byline-template-rtl.css', array(), AUTHOR_KIT_VERSION, 'all');
            }
            break;
    }
}

/**
 * Loads the inline styles for the active byline template.
 *
 * Retrieves the active template from the database and enqueues its stylesheet.
 * Additionally, generates and applies custom inline CSS.
 */
function author_kit_load_byline_inline_styles()
{
    if (is_archive() || ! (is_single() || is_page())) {
        return;
    }

    $active_template = get_option('author_kit_custom_byline_template', 'byline-default');

    // Enqueue styles for the active template
    author_kit_enqueue_byline_styles($active_template);

    // Generate and add custom inline CSS
    $custom_css = author_kit_generate_byline_inline_css();
    if (! empty($custom_css)) {

        // Escaping custom inline CSS with wp_kses to prevent XSS.
        // CSS is user-defined via Kirki controls and sanitized before being stored.
        // We escape again here (late) to ensure secure rendering.
        wp_add_inline_style("author-kit-$active_template",  wp_kses($custom_css, array('style' => array('type' => true))));
    }
}

// Hook into 'wp_enqueue_scripts' to load styles and inline CSS for the active template
add_action('wp_enqueue_scripts', 'author_kit_load_byline_inline_styles', 9999);
