<?php
/**
 * Server-side rendering for Author Pro Featured In Block
 */

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : 'featured-in-' . uniqid();
$section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'Featured In';
$logos = isset($attributes['logos']) ? $attributes['logos'] : [];
$padding_top = isset($attributes['paddingTop']) ? $attributes['paddingTop'] : 32;
$padding_bottom = isset($attributes['paddingBottom']) ? $attributes['paddingBottom'] : 32;
$bg_color = isset($attributes['bgColor']) ? $attributes['bgColor'] : 'bg-white';
$anchor = isset($attributes['anchor']) ? $attributes['anchor'] : '';
// Handle dynamic background color vs utility class
$inline_styles = [];
$inline_styles[] = "padding-top: {$padding_top}px;";
$inline_styles[] = "padding-bottom: {$padding_bottom}px;";

if (strpos($bg_color, 'bg-') === false && !empty($bg_color)) {
    $inline_styles[] = "background-color: {$bg_color};";
    $bg_class = ''; 
} else {
    $bg_class = $bg_color;
}

$style_attr = !empty($inline_styles) ? 'style="' . implode(' ', $inline_styles) . '"' : '';

?>

<section <?php echo $anchor ? 'id="' . esc_attr($anchor) . '"' : ''; ?> class="awt-featured-in py-10 bg-white border-b border-gray-100 <?php echo esc_attr($bg_class); ?> block-<?php echo esc_attr($block_id); ?>" <?php echo $style_attr; ?>>
    <div class="awt-container container mx-auto px-4">
        <div class="flex flex-col items-center justify-between gap-6">
            <!-- Title -->
            <div class="text-white/40 font-serif italic text-lg whitespace-nowrap">
                <p class="text-xs font-bold text-gray-400 uppercase tracking-widest mb-6"><?php echo wp_kses_post($section_title); ?></p>
            </div>

            <!-- Logos -->
            <div class="flex flex-wrap justify-center md:justify-end gap-12 opacity-50">
                <?php foreach ($logos as $logo): ?>
                    <?php 
                        $has_link = !empty($logo['link']) && $logo['link'] !== '#';
                        $logo_text = isset($logo['text']) ? $logo['text'] : '';
                    ?>
                    
                    <?php if ($has_link): ?>
                        <a href="<?php echo esc_url($logo['link']); ?>" target="_blank" rel="nofollow noopener" class="transition hover:opacity-100 group">
                    <?php else: ?>
                        <div class="group">
                    <?php endif; ?>

                        <?php if (!empty($logo['url'])): ?>
                            <img 
                                src="<?php echo esc_url($logo['url']); ?>" 
                                alt="<?php echo esc_attr(isset($logo['alt']) ? $logo['alt'] : $logo_text); ?>" 
                                class="h-8 w-auto grayscale group-hover:grayscale-0 transition duration-300"
                            >
                        <?php else: ?>
                            <span class="text-2xl font-serif font-bold">
                                <?php echo esc_html($logo_text); ?>
                            </span>
                        <?php endif; ?>

                    <?php if ($has_link): ?>
                        </a>
                    <?php else: ?>
                        </div>
                    <?php endif; ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</section>
