<?php
// Monthly Horoscope Game Shortcode
defined('ABSPATH') or die('No!');

function tarotxhoroscope_monthly_horoscope_shortcode($atts) {
    $atts = shortcode_atts(array('sign' => 'aries'), $atts);
    $sign = strtolower(sanitize_text_field($atts['sign']));
    $valid_signs = ['aries', 'taurus', 'gemini', 'cancer', 'leo', 'virgo', 'libra', 'scorpio', 'sagittarius', 'capricorn', 'aquarius', 'pisces'];
    if (!in_array($sign, $valid_signs)) {
        return '<p>Invalid star sign</p>';
    }

    $settings = get_option('tarotxhoroscope_horoscope_settings', array(
        'font_family' => 'Tahoma',
        'font_size' => '16px',
        'text_color' => '#000000',
        'image_shown' => 1,
        'image_position' => 'top',
        'show_date' => 1,
        'border_enabled' => 0,
        'border_color' => '#000000',
        'border_width' => 1,
        'border_radius' => 0,
        'width' => 400,
    ));
    $version = '1.0.4';

    $unique_id = 'horoscope-' . $sign . '-' . uniqid();

    wp_enqueue_style('horoscope-styles', TAROTXHOROSCOPE_PLUGIN_URL . 'games/css/horoscopestyles.css', array(), $version);
    wp_enqueue_script('horoscope-js', TAROTXHOROSCOPE_PLUGIN_URL . 'games/js/monthly_horoscope.js', array('jquery'), $version, true);
    wp_localize_script('horoscope-js', 'horoscopeData', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('horoscope_nonce'),
        'image_shown' => (int) $settings['image_shown'],
        'image_position' => $settings['image_position'],
        'show_date' => (int) $settings['show_date'],
        'image_url' => TAROTXHOROSCOPE_PLUGIN_URL . 'games/images/horoscopes/' . $sign . '.png',
    ));

    ob_start();
?>
    <div id="<?php echo esc_attr($unique_id); ?>" class="horoscope-container" data-sign="<?php echo esc_attr($sign); ?>" style="width: <?php echo esc_attr($settings['width']); ?>px; font-family: <?php echo esc_attr($settings['font_family']); ?>; font-size: <?php echo esc_attr($settings['font_size']); ?>; color: <?php echo esc_attr($settings['text_color']); ?>; <?php echo $settings['border_enabled'] ? 'border: ' . esc_attr($settings['border_width']) . 'px solid ' . esc_attr($settings['border_color']) . '; border-radius: ' . esc_attr($settings['border_radius']) . 'px;' : ''; ?> padding: 10px; overflow: hidden; text-align: center;">
        <div id="<?php echo esc_attr($unique_id); ?>-content" class="horoscope-content">Loading horoscope...</div>
    </div>
<?php
    return ob_get_clean();
}

add_shortcode('tarotxhoroscope_monthly_horoscope', 'tarotxhoroscope_monthly_horoscope_shortcode');