<?php
if (!defined('ABSPATH'))
    exit;

$enabled = (bool) get_theme_mod('pawfect_instagram_gallery_enabled', false);
$mode = get_theme_mod('pawfect_instagram_mode', __( 'api', 'pawfect' ));
$heading = get_theme_mod('pawfect_instagram_heading_text', '@PAWS');
$subtext = get_theme_mod('pawfect_instagram_subtext', __( 'FOLLOW US ON INSTAGRAM', 'pawfect' ));
$container = get_theme_mod('pawfect_instagram_gallery_container_size', 'container');

// API mode settings
$access_token = get_theme_mod('pawfect_instagram_access_token');
$user_id = get_theme_mod('pawfect_instagram_user_id');

// Manual images
$manual_images_row = get_theme_mod('pawfect_instagram_manual_images');

$manual_images = [];
if (!empty($manual_images_row)) {
    $decoded = json_decode($manual_images_row);
    if (is_array($decoded)) {
        $manual_images = $decoded;
    }
}



// Fetch from API
$media_items = ($mode === 'api' && $access_token && $user_id)
    ? Helpers::get_instagram_media($user_id, $access_token, 8)
    : [];

// Exit early if section is disabled
if (!$enabled)
    return;
?>

<!-- Enhanced Instagram Feed Section -->
<section class="pawfect-instagram-section pawfect-relative pawfect-bg-pawfect-background pawfect-overflow-hidden pawfect-w-full pawfect-py-20 lg:pawfect-py-28">

   

    <!-- Background Decorations -->
    <div class="pawfect-absolute pawfect-inset-0 pawfect-opacity-5">
        <div class="pawfect-absolute pawfect-w-96 pawfect-h-96 pawfect-bg-pawfect-primary pawfect-rounded-full pawfect-blur-3xl pawfect--top-48 pawfect--left-48 pawfect-animate-pulse"></div>
        <div class="pawfect-absolute pawfect-w-64 pawfect-h-64 pawfect-bg-gray-400 pawfect-rounded-full pawfect-blur-2xl pawfect--bottom-32 pawfect--right-32 pawfect-animate-pulse pawfect-animation-delay-2000"></div>
    </div>

    <!-- Enhanced Image Gallery -->
    <div class="pawfect-relative pawfect-z-10 pawfect-flex pawfect-gap-2  pawfect-scrollbar-hide">
        <div class="pawfect-flex pawfect-w-full pawfect-gap-2">
            <?php if ($mode === 'manual' && is_array($manual_images)): ?>
                <?php foreach ($manual_images as $index => $item):
                    $image = $item->image_url ?? '';
                    $link = $item->link ?? '';
                    $open_new_tab = !empty($item->open_new_tab) && $item->open_new_tab === 'yes' ? '_blank' : '_self';
                    if (!$image) continue;
                    ?>
                    <!-- Enhanced Instagram Image -->
                    <div class="pawfect-instagram-item pawfect-group pawfect-relative pawfect-w-1/4 pawfect-h-[300px] pawfect-overflow-hidden pawfect-rounded-2xl pawfect-shadow-lg pawfect-transition-all pawfect-duration-500 hover:pawfect-shadow-2xl hover:pawfect-scale-105"
                         style="animation-delay: <?php echo absint( $index * 100 ); ?>ms;">

                        <a href="<?php echo esc_url($link) ?: '#'; ?>" target="<?php echo esc_attr($open_new_tab); ?>" <?php echo esc_attr( $open_new_tab === '_blank' ? 'rel="noopener noreferrer"' : '' ); ?> class="pawfect-block pawfect-w-full pawfect-h-full">
                            <!-- Image -->
                            <img src="<?php echo esc_url($image); ?>"
                                 alt="<?php esc_attr_e( 'Instagram Image', 'pawfect' ); ?>"
                                 class="pawfect-w-full pawfect-h-full pawfect-object-cover pawfect-transition-all pawfect-duration-700 group-hover:pawfect-scale-110" />

                            <!-- Hover Overlay -->
                            <div class="pawfect-absolute pawfect-inset-0 pawfect-bg-gradient-to-t pawfect-from-black pawfect-from-opacity-60 pawfect-to-transparent pawfect-opacity-0 pawfect-transition-opacity pawfect-duration-300 group-hover:pawfect-opacity-100"></div>

                            <!-- Instagram Icon -->
                            <div class="pawfect-absolute pawfect-inset-0 pawfect-flex pawfect-items-center pawfect-justify-center pawfect-opacity-0 pawfect-transform pawfect-scale-75 pawfect-transition-all pawfect-duration-300 group-hover:pawfect-opacity-100 group-hover:pawfect-scale-100">
                                <div class="pawfect-w-12 pawfect-h-12 pawfect-bg-white pawfect-bg-opacity-90 pawfect-backdrop-blur-sm pawfect-rounded-full pawfect-flex pawfect-items-center pawfect-justify-center pawfect-shadow-lg">
                                    <svg class="pawfect-w-6 pawfect-h-6 pawfect-text-pink-500" fill="currentColor" viewBox="0 0 24 24">
                                        <path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0-2.163c-3.259 0-3.667.014-4.947.072-4.358.2-6.78 2.618-6.98 6.98-.059 1.281-.073 1.689-.073 4.948 0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98 1.281.058 1.689.072 4.948.072 3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98-1.281-.059-1.69-.073-4.949-.073zm0 5.838c-3.403 0-6.162 2.759-6.162 6.162s2.759 6.163 6.162 6.163 6.162-2.759 6.162-6.163c0-3.403-2.759-6.162-6.162-6.162zm0 10.162c-2.209 0-4-1.79-4-4 0-2.209 1.791-4 4-4s4 1.791 4 4c0 2.21-1.791 4-4 4zm6.406-11.845c-.796 0-1.441.645-1.441 1.44s.645 1.44 1.441 1.44c.795 0 1.439-.645 1.439-1.44s-.644-1.44-1.439-1.44z"/>
                                    </svg>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php endforeach; ?>

            <?php elseif ($mode === 'api' && !empty($media_items)): ?>
                <?php foreach ($media_items as $index => $media): ?>
                    <!-- Enhanced Instagram Image (API) -->
                    <div class="pawfect-instagram-item pawfect-group pawfect-relative pawfect-w-1/4 pawfect-h-[300px] pawfect-overflow-hidden pawfect-rounded-2xl pawfect-shadow-lg pawfect-transition-all pawfect-duration-500 hover:pawfect-shadow-2xl hover:pawfect-scale-105"
                         style="animation-delay: <?php echo absint( $index * 100 ); ?>ms;">

                        <a href="<?php echo esc_url($media->permalink); ?>" target="_blank" rel="noopener noreferrer" class="pawfect-block pawfect-w-full pawfect-h-full">
                            <!-- Image -->
                            <img src="<?php echo esc_url($media->media_url); ?>"
                                 alt="<?php esc_attr_e( 'Instagram Image', 'pawfect' ); ?>"
                                 class="pawfect-w-full pawfect-h-full pawfect-object-cover pawfect-transition-all pawfect-duration-700 group-hover:pawfect-scale-110" />

                            <!-- Hover Overlay -->
                            <div class="pawfect-absolute pawfect-inset-0 pawfect-bg-gradient-to-t pawfect-from-black pawfect-from-opacity-60 pawfect-to-transparent pawfect-opacity-0 pawfect-transition-opacity pawfect-duration-300 group-hover:pawfect-opacity-100"></div>

                            <!-- Instagram Icon -->
                            <div class="pawfect-absolute pawfect-inset-0 pawfect-flex pawfect-items-center pawfect-justify-center pawfect-opacity-0 pawfect-transform pawfect-scale-75 pawfect-transition-all pawfect-duration-300 group-hover:pawfect-opacity-100 group-hover:pawfect-scale-100">
                                <div class="pawfect-w-12 pawfect-h-12 pawfect-bg-white pawfect-bg-opacity-90 pawfect-backdrop-blur-sm pawfect-rounded-full pawfect-flex pawfect-items-center pawfect-justify-center pawfect-shadow-lg">
                                    <svg class="pawfect-w-6 pawfect-h-6 pawfect-text-pink-500" fill="currentColor" viewBox="0 0 24 24">
                                        <path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0-2.163c-3.259 0-3.667.014-4.947.072-4.358.2-6.78 2.618-6.98 6.98-.059 1.281-.073 1.689-.073 4.948 0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98 1.281.058 1.689.072 4.948.072 3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98-1.281-.059-1.69-.073-4.949-.073zm0 5.838c-3.403 0-6.162 2.759-6.162 6.162s2.759 6.163 6.162 6.163 6.162-2.759 6.162-6.163c0-3.403-2.759-6.162-6.162-6.162zm0 10.162c-2.209 0-4-1.79-4-4 0-2.209 1.791-4 4-4s4 1.791 4 4c0 2.21-1.791 4-4 4zm6.406-11.845c-.796 0-1.441.645-1.441 1.44s.645 1.44 1.441 1.44c.795 0 1.439-.645 1.439-1.44s-.644-1.44-1.439-1.44z"/>
                                    </svg>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <!-- Enhanced Overlay Text -->
        <div class="pawfect-absolute pawfect-inset-0 pawfect-flex pawfect-items-center pawfect-justify-center pawfect-z-20">
            <div class="pawfect-instagram-heading pawfect-group pawfect-bg-white pawfect-bg-opacity-90 pawfect-backdrop-blur-lg pawfect-p-8 lg:pawfect-p-12 pawfect-rounded-2xl pawfect-text-center  pawfect-border pawfect-border-white pawfect-border-opacity-50 ">

               

                <!-- Enhanced Heading -->
                <h2 class="pawfect-text-2xl md:pawfect-text-3xl lg:pawfect-text-3xl pawfect-font-bold pawfect-text-[#1f2937] pawfect-mb-4 pawfect-leading-tight pawfect-tracking-tight pawfect-transition-all pawfect-duration-300 group-hover:pawfect-text-pawfect-primary">
                    <?php echo esc_html($heading); ?>
                </h2>

                <!-- Enhanced Subtext -->
                <p class="pawfect-text-lg pawfect-text-[#4b5563] pawfect-font-medium pawfect-tracking-wider pawfect-uppercase ">
                    <?php echo esc_html($subtext); ?>
                </p>

                
            </div>
        </div>
    </div>
</section>