<?php
/**
 * Dynamic Styles - Centralized Inline CSS Output
 *
 * This file outputs all dynamic CSS styles to the wp_head.
 * Centralizing dynamic styles prevents auto-formatter issues
 * and makes maintenance easier.
 *
 * @package Paws_And_Care
 * @since 0.1.2
 */

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

/**
 * Output dynamic inline styles to wp_head
 */
function paws_and_care_dynamic_styles()
{
    $css = '';

    // ========================================
    // SLIDER SECTION STYLES
    // ========================================

    // Slider Caption Background
    $slider_caption_bg = get_theme_mod('paws_and_care_slider_caption_bg', 'rgba(0,0,0,0.5)');
    if (!empty($slider_caption_bg)) {
        $css .= '#carouselSlider .carousel-caption { background: ' . esc_attr($slider_caption_bg) . ' !important; }';
    }

    // Slider Height (default is 100vh, only output if different)
    $slider_height = absint(get_theme_mod('paws_and_care_slider_height', 100));
    if (!empty($slider_height)) {
        $css .= '.slider-section .carousel, .slider-section .carousel-inner, .slider-section .carousel-item { height: ' . esc_attr($slider_height) . 'vh !important; }';
    }

    // Slider Image Position (default is center, only output if different)
    $slider_image_position = get_theme_mod('paws_and_care_slider_image_position', 'bottom');
    if (!empty($slider_image_position)) {
        $css .= '.slider-section .carousel-item img { object-position: center ' . esc_attr($slider_image_position) . ' !important; }';
    }

    // ========================================
    // COLOR SETTINGS - CSS VARIABLES
    // ========================================

    // Get all color settings (using new setting names)
    $theme_color = get_theme_mod('paws_and_care_theme_color', '#ff8c42');
    $enable_gradients = get_theme_mod('paws_and_care_enable_gradients', true);
    $gradient_end_color = get_theme_mod('paws_and_care_gradient_end_color', '#ffa726');
    $secondary_color = get_theme_mod('paws_and_care_secondary_color', '#4a90e2');
    $body_text_color = get_theme_mod('paws_and_care_body_text_color', '#222a33');
    $heading_color = get_theme_mod('paws_and_care_heading_color', '#222a33');
    $link_color = get_theme_mod('paws_and_care_link_color', '#ff8a4c');
    $link_hover_color = get_theme_mod('paws_and_care_link_hover_color', '#ff6b35');
    $body_bg_color = get_theme_mod('paws_and_care_body_bg_color', '#f8f9fa');
    $content_bg_color = get_theme_mod('paws_and_care_content_bg_color', '#ffffff');
    $btn_bg_color = get_theme_mod('paws_and_care_btn_bg_color', '#ff8c42');
    $btn_text_color = get_theme_mod('paws_and_care_btn_text_color', '#ffffff');
    $btn_hover_bg_color = get_theme_mod('paws_and_care_btn_hover_bg_color', '#ff6b35');
    $btn_hover_text_color = get_theme_mod('paws_and_care_btn_hover_text_color', '#ffffff');

    // Build CSS variables - override ALL theme :root variables for full control
    $css .= ':root {';
    // Core colors
    $css .= '--primary-color: ' . esc_attr($theme_color) . ';';
    $css .= '--secondary-color: ' . esc_attr($secondary_color) . ';';
    $css .= '--accent-color: ' . esc_attr($gradient_end_color) . ';';
    $css .= '--tertiary-color: ' . esc_attr($gradient_end_color) . ';'; // Alias
    $css .= '--text-color: ' . esc_attr($body_text_color) . ';';
    $css .= '--text-dark: ' . esc_attr($body_text_color) . ';';
    $css .= '--dark-color: ' . esc_attr($heading_color) . ';';
    $css .= '--heading-color: ' . esc_attr($heading_color) . ';';
    $css .= '--light-color: ' . esc_attr($body_bg_color) . ';';
    $css .= '--body-bg: ' . esc_attr($body_bg_color) . ';';
    $css .= '--content-bg: ' . esc_attr($content_bg_color) . ';';

    // Gradient colors - conditional: gradient or solid based on toggle
    if ($enable_gradients) {
        $css .= '--gradient-primary: linear-gradient(135deg, ' . esc_attr($theme_color) . ' 0%, ' . esc_attr($gradient_end_color) . ' 100%);';
        $css .= '--gradient-secondary: linear-gradient(135deg, ' . esc_attr($secondary_color) . ' 0%, ' . esc_attr($gradient_end_color) . ' 100%);';
    } else {
        // Solid colors when gradients disabled
        $css .= '--gradient-primary: ' . esc_attr($theme_color) . ';';
        $css .= '--gradient-secondary: ' . esc_attr($secondary_color) . ';';
    }

    // Link colors 
    $css .= '--link-color: ' . esc_attr($link_color) . ';';
    $css .= '--link-hover-color: ' . esc_attr($link_hover_color) . ';';
    $css .= '--bs-link-color: ' . esc_attr($link_color) . ';';
    $css .= '--bs-link-hover-color: ' . esc_attr($link_hover_color) . ';';

    // Button colors
    $css .= '--btn-bg: ' . esc_attr($btn_bg_color) . ';';
    $css .= '--btn-text: ' . esc_attr($btn_text_color) . ';';
    $css .= '--btn-hover-bg: ' . esc_attr($btn_hover_bg_color) . ';';
    $css .= '--btn-hover-text: ' . esc_attr($btn_hover_text_color) . ';';

    // Bootstrap overrides for full theme integration
    $css .= '--bs-primary: ' . esc_attr($theme_color) . ';';
    $css .= '--bs-pagination-color: ' . esc_attr($link_color) . ';';
    $css .= '--bs-pagination-hover-color: ' . esc_attr($link_hover_color) . ';';
    $css .= '--bs-pagination-active-bg: ' . esc_attr($theme_color) . ';';
    $css .= '--bs-pagination-active-border-color: ' . esc_attr($theme_color) . ';';
    $css .= '}';

    // Apply colors to elements
    $css .= 'body { background-color: var(--body-bg); color: var(--text-color); }';
    $css .= 'h1, h2, h3, h4, h5, h6 { color: var(--heading-color); }';
    $css .= 'a { color: var(--link-color); }';
    $css .= 'a:hover { color: var(--link-hover-color); }';
    $css .= '.btn-primary { background-color: var(--btn-bg) !important; border-color: var(--btn-bg) !important; color: var(--btn-text) !important; }';
    $css .= '.btn-primary:hover, .btn-primary:focus { background-color: var(--btn-hover-bg) !important; border-color: var(--btn-hover-bg) !important; color: var(--btn-hover-text) !important; }';
    $css .= '.card, .content-area, .widget { background-color: var(--content-bg); }';

    // Feature icon circles and gradient elements
    $css .= '.feature-icon-circle { background: var(--gradient-primary); }';
    $css .= '.scroll-to-top { background: var(--gradient-primary); }';

    // Secondary color usages
    $css .= '.btn-secondary, .badge-secondary { background-color: var(--secondary-color); border-color: var(--secondary-color); }';

    // Accent color usages
    $css .= 'blockquote { border-left: 4px solid var(--accent-color); padding-left: 1rem; }';
    $css .= '.badge-accent, .tag-accent { background-color: var(--accent-color); color: #fff; }';
    $css .= '::selection { background-color: var(--accent-color); color: #fff; }';
    $css .= '.section-subtitle, .lead strong { color: var(--primary-color); }';
    $css .= '.icon-box:hover, .feature-box:hover { border-color: var(--primary-color); }';
    $css .= '.price-highlight, .featured-price { color: var(--primary-color); font-weight: 600; }';

    // Global background color applied to ALL sections, pages, and page templates
    $css .= '.services-section, .about-section, .team-section, .testimonials-section, .pricing-section, .portfolio-section, .blog-section, .clients-section, .faq-section, .contact-section { background-color: var(--body-bg); }';
    $css .= '.bg-light { background-color: var(--body-bg); }';
    $css .= '.widget-area .widget, .sidebar .widget { background-color: var(--body-bg); }';
    $css .= 'section { background-color: var(--body-bg); }';
    $css .= '.page-header-section { background-color: var(--body-bg); }';
    
    // WooCommerce Specific Dynamic Styles
    if (class_exists('WooCommerce')) {
        $css .= '.woocommerce span.onsale, .woocommerce-page span.onsale { background-color: var(--primary-color) !important; }';
        $css .= '.woocommerce .star-rating span::before, .woocommerce-page .star-rating span::before { color: var(--primary-color); }';
        $css .= '.woocommerce-message, .woocommerce-info { border-top-color: var(--primary-color); }';
        $css .= '.woocommerce-message::before, .woocommerce-info::before { color: var(--primary-color); }';
    }

    // Output the CSS using wp_add_inline_style (WordPress recommended)
    if (!empty($css)) {
        wp_add_inline_style('paws-and-care-style', $css);
    }
}
add_action('wp_enqueue_scripts', 'paws_and_care_dynamic_styles', 20);
