<?php
/**
 * Event sponsors component
 */
if (!defined('ABSPATH')) exit;

// Get sponsor settings
$em_show_sponsor = $event_settings['em_show_event_sponsor'] ?? 'no';
$em_sponsor_heading = $event_settings['em_sponsor_heading'] ?? 'Event Sponsors';
$em_sponsor_name = $event_settings['em-slide-spr-names'] ?? array();
$em_sponsor_website = $event_settings['em-slide-spr-links'] ?? array();
$em_sponsor_logo = $event_settings['em-slide-spr-ids'] ?? array();

// Only show if sponsors are enabled and have data
if ($em_show_sponsor !== 'yes' ) {
    return;
}

// Check if magazine layout
$layout_preference = $event_settings['em_layout_preference'] ?? 'default';
$rounded_class = ($layout_preference === 'magazine') ? 'em-rounded-2xl' : '';
?>
<section class="em-bg-white em-p-5 em-border em-border-slate-200 em-shadow-sm em-animate-fade-in em-animate-delay-600 <?php echo esc_attr($rounded_class); ?>">
    <h2 class="em-text-lg em-font-bold em-text-slate-900 em-mb-4 em-text-center">Event Partners</h2>
    <div class="em-flex em-justify-center em-items-center em-gap-8 em-flex-wrap">
        <?php
        // Use default sponsor logos if no custom sponsors
        if (empty($em_sponsor_name) || empty(array_filter($em_sponsor_name))) {
            $default_sponsors = array('InnovateCo', 'DesignHub', 'DevTools');

            foreach ($default_sponsors as $name) {
                ?>
                <span class="em-inline-block em-px-4 em-py-2 em-bg-slate-100 em-text-slate-500 em-font-semibold em-rounded-md em-text-sm"><?php echo esc_html($name); ?></span>
                <?php
            }
        } else {
            $sponsor_count = 0;
            $max_sponsors = 3; // Limit to 3 sponsors for the design

            foreach ($em_sponsor_name as $key => $name) {
                if ($sponsor_count >= $max_sponsors) break;
                if (empty(trim($name))) continue;

                $website = $em_sponsor_website[$key] ?? '';
                $logo = $em_sponsor_logo[$key] ?? '';
                $logo_url = !empty($logo) ? wp_get_attachment_image_url($logo, 'medium') : '';

                $sponsor_count++;
                ?>
                <?php if ($logo_url): ?>
                    <?php if (!empty($website)): ?>
                        <a href="<?php echo esc_url($website); ?>" target="_blank" rel="noopener noreferrer">
                            <img src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr($name); ?>" class="em-h-7 em-object-contain">
                        </a>
                    <?php else: ?>
                        <img src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr($name); ?>" class="em-h-7 em-object-contain">
                    <?php endif; ?>
                <?php else: ?>
                    <span class="em-inline-block em-px-4 em-py-2 em-bg-slate-100 em-text-slate-500 em-font-semibold em-rounded-md em-text-sm"><?php echo esc_html($name); ?></span>
                <?php endif; ?>
                <?php
            }
        }
        ?>
    </div>
</section>
