<?php
/**
 * Horoscope Summaries Shortcode
 * 
 * Register and handle the [ezhoroscope_summaries] shortcode
 * 
 * @package EZ_Horoscope
 */

// Exit if accessed directly
defined('ABSPATH') || exit;

/**
 * Register the shortcode
 */
add_action('init', 'ezhp_register_summaries_shortcode');

function ezhp_register_summaries_shortcode() {
    add_shortcode('ezhoroscope_summaries', 'ezhp_render_summaries_shortcode');
}

/**
 * Render the shortcode
 * 
 * Example usage:
 * [ezhoroscope_summaries language="es"]
 * [ezhoroscope_summaries language="fr" hero_mode="background"]
 * [ezhoroscope_summaries language="en" show_header="false"]
 * [ezhoroscope_summaries language="en" link_aries="https://example.com/aries" image_aries="https://example.com/aries.jpg"]
 * 
 * @param array $atts Shortcode attributes
 * @return string Rendered HTML output
 */
function ezhp_render_summaries_shortcode($atts) {
    // Default attributes
    $ezhp_defaults = [
        'language' => 'en',
        'show_header' => 'true',
        
        // Image attributes
        'image_aries' => '',
        'image_taurus' => '',
        'image_gemini' => '',
        'image_cancer' => '',
        'image_leo' => '',
        'image_virgo' => '',
        'image_libra' => '',
        'image_scorpio' => '',
        'image_sagittarius' => '',
        'image_capricorn' => '',
        'image_aquarius' => '',
        'image_pisces' => '',
        
        // Link attributes
        'link_aries' => '',
        'link_taurus' => '',
        'link_gemini' => '',
        'link_cancer' => '',
        'link_leo' => '',
        'link_virgo' => '',
        'link_libra' => '',
        'link_scorpio' => '',
        'link_sagittarius' => '',
        'link_capricorn' => '',
        'link_aquarius' => '',
        'link_pisces' => '',
        
        // Hero settings
        'hero_mode' => 'inline',
        'hero_wrap' => 'none',
        'hero_align' => 'center',
        'hero_opacity' => 50,
        'hero_color' => 'black',
        'hero_max_width' => '40%',
    ];
    
    // Parse shortcode attributes
    $ezhp_atts = shortcode_atts($ezhp_defaults, $atts, 'ezhoroscope_summaries');
    
    // Convert show_header string to boolean
    $ezhp_show_header = filter_var($ezhp_atts['show_header'], FILTER_VALIDATE_BOOLEAN);
    
    // Convert shortcode attributes to block attributes format
    $ezhp_block_attributes = [
        'language' => $ezhp_atts['language'],
        'showHeader' => $ezhp_show_header,
        
        // Images
        'imageAries' => $ezhp_atts['image_aries'],
        'imageTaurus' => $ezhp_atts['image_taurus'],
        'imageGemini' => $ezhp_atts['image_gemini'],
        'imageCancer' => $ezhp_atts['image_cancer'],
        'imageLeo' => $ezhp_atts['image_leo'],
        'imageVirgo' => $ezhp_atts['image_virgo'],
        'imageLibra' => $ezhp_atts['image_libra'],
        'imageScorpio' => $ezhp_atts['image_scorpio'],
        'imageSagittarius' => $ezhp_atts['image_sagittarius'],
        'imageCapricorn' => $ezhp_atts['image_capricorn'],
        'imageAquarius' => $ezhp_atts['image_aquarius'],
        'imagePisces' => $ezhp_atts['image_pisces'],
        
        // Links
        'linkAries' => $ezhp_atts['link_aries'],
        'linkTaurus' => $ezhp_atts['link_taurus'],
        'linkGemini' => $ezhp_atts['link_gemini'],
        'linkCancer' => $ezhp_atts['link_cancer'],
        'linkLeo' => $ezhp_atts['link_leo'],
        'linkVirgo' => $ezhp_atts['link_virgo'],
        'linkLibra' => $ezhp_atts['link_libra'],
        'linkScorpio' => $ezhp_atts['link_scorpio'],
        'linkSagittarius' => $ezhp_atts['link_sagittarius'],
        'linkCapricorn' => $ezhp_atts['link_capricorn'],
        'linkAquarius' => $ezhp_atts['link_aquarius'],
        'linkPisces' => $ezhp_atts['link_pisces'],
        
        // Hero settings
        'heroImageDisplayMode' => $ezhp_atts['hero_mode'],
        'heroImageTextWrap' => $ezhp_atts['hero_wrap'],
        'heroImageAlignment' => $ezhp_atts['hero_align'],
        'heroImageOverlayOpacity' => floatval($ezhp_atts['hero_opacity']),
        'heroImageOverlayColor' => $ezhp_atts['hero_color'],
        'heroImageMaxWidth' => $ezhp_atts['hero_max_width'],
    ];
    
    // Start output buffering
    ob_start();
    
    // Get plugin root directory (go up one level from 'shortcodes' folder)
    $ezhp_plugin_dir = plugin_dir_path(dirname(__FILE__));
    
    // Path to the block's render file
    $ezhp_render_file = $ezhp_plugin_dir . 'build/blocks/horoscope-summaries/render.php';
    
    // Check if render file exists
    if (file_exists($ezhp_render_file)) {
        // Set $attributes variable for the render file
        $attributes = $ezhp_block_attributes;
        
        // Include the render file
        include $ezhp_render_file;
    } else {
        echo '<div class="ez-horoscope-block-error">Horoscope summaries render file not found.</div>';
    }
    
    // Return the buffered output
    return ob_get_clean();
}