<?php
/**
 * Plugin Name: Team Overlay
 * Plugin URI:  https://bowdenworks.com
 * Description: Displays a Team Grid where the AI Avatar swaps to the Real Photo on hover.
 * Version:     1.2.0
 * Author:      Bowden Works
 * Author URI:  https://bowdenworks.com
 * License:     GPL2
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
 * Shortcode: [team_overlay]
 */
function bw_team_overlay_shortcode( $atts ) {
    
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'posts_per_page' => -1,
        'columns'        => 3,
        'gap'            => '20px'
    ), $atts );

    ob_start();

    // The Query
    $args = array(
        'post_type'      => 'team',
        'posts_per_page' => intval( $atts['posts_per_page'] ),
        'post_status'    => 'publish',
        'orderby'        => 'menu_order title',
        'order'          => 'ASC',
    );

    $team_query = new WP_Query( $args );

    if ( $team_query->have_posts() ) : ?>
        
        <style>
            .bw-team-grid {
                display: grid;
                grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
                gap: <?php echo esc_attr( $atts['gap'] ); ?>;
                margin-bottom: 40px;
            }

            /* The Card Link Wrapper */
            a.bw-team-card {
                display: block;
                position: relative;
                overflow: hidden;
                border-radius: 12px;
                box-shadow: 0 4px 10px rgba(0,0,0,0.1);
                aspect-ratio: 1 / 1; /* Force Square */
                background-color: #f0f0f0;
                text-decoration: none; 
                color: inherit;
                /* Removed 'transform' to stop movement */
                transition: box-shadow 0.3s ease;
            }

            /* Card Hover Effect */
            a.bw-team-card:hover {
                /* Only increase shadow slightly, no movement */
                box-shadow: 0 8px 16px rgba(0,0,0,0.15);
            }

            /* Image Container */
            .bw-team-image-wrapper {
                position: absolute;
                width: 100%;
                height: 100%;
                top: 0;
                left: 0;
                z-index: 1;
            }

            /* Shared Image Styles */
            .bw-team-image-wrapper img {
                width: 100%;
                height: 100%;
                object-fit: cover;
                position: absolute;
                top: 0;
                left: 0;
                transition: opacity 0.5s ease-in-out; 
            }

            /* Layering Logic */
            .bw-img-real {
                opacity: 0; /* Hidden by default */
                z-index: 2;
            }
            .bw-img-avatar {
                opacity: 1; /* Visible by default */
                z-index: 1;
            }

            /* SWAP IMAGE ON HOVER */
            a.bw-team-card:hover .bw-img-real {
                opacity: 1;
            }

            /* Text Content (Always Visible) */
            .bw-team-info {
                position: absolute;
                bottom: 0;
                left: 0;
                right: 0;
                padding: 20px 15px;
                background: linear-gradient(to top, rgba(0,0,0,0.85) 0%, rgba(0,0,0,0) 100%); 
                z-index: 10; 
                color: #fff;
                text-align: left;
            }

            .bw-team-name {
                display: block;
                font-size: 1.25rem;
                font-weight: 700;
                line-height: 1.2;
                margin-bottom: 4px;
                text-shadow: 0 2px 4px rgba(0,0,0,0.5);
            }

            .bw-team-job {
                display: block;
                font-size: 0.9rem;
                font-weight: 400;
                opacity: 0.9;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                text-shadow: 0 1px 2px rgba(0,0,0,0.5);
            }
        </style>

        <div class="bw-team-grid">
            <?php while ( $team_query->have_posts() ) : $team_query->the_post(); 
                
                // DATA: Images
                $featured_id  = get_post_thumbnail_id(); 
                $avatar_field = get_post_meta( get_the_ID(), 'avatar', true );
                
                // DATA: Text
                $job_title = get_post_meta( get_the_ID(), 'job_title', true );
                $team_name = get_the_title();
                $team_link = get_permalink();

                // Normalize Avatar Data
                $avatar_id = '';
                $avatar_url = '';
                if ( is_numeric( $avatar_field ) ) {
                    $avatar_id = $avatar_field;
                } elseif ( is_array( $avatar_field ) && isset( $avatar_field['ID'] ) ) {
                    $avatar_id = $avatar_field['ID']; 
                } elseif ( is_string( $avatar_field ) ) {
                    $avatar_url = $avatar_field;
                }

                $has_featured = ! empty( $featured_id );
                $has_avatar   = ! empty( $avatar_id ) || ! empty( $avatar_url );
                ?>

                <a href="<?php echo esc_url($team_link); ?>" class="bw-team-card">
                    
                    <div class="bw-team-image-wrapper">
                        <?php 
                        // 1. Both Images exist
                        if ( $has_avatar && $has_featured ) : 
                            if ( $avatar_id ) echo wp_get_attachment_image( $avatar_id, 'large', false, ['class' => 'bw-img-avatar'] );
                            else echo '<img src="' . esc_url( $avatar_url ) . '" class="bw-img-avatar" alt="Avatar">';
                            
                            echo wp_get_attachment_image( $featured_id, 'large', false, ['class' => 'bw-img-real'] );

                        // 2. Only Avatar
                        elseif ( $has_avatar && ! $has_featured ) :
                             if ( $avatar_id ) echo wp_get_attachment_image( $avatar_id, 'large', false, ['class' => 'bw-img-avatar'] );
                             else echo '<img src="' . esc_url( $avatar_url ) . '" class="bw-img-avatar" alt="Avatar">';

                        // 3. Only Real Photo
                        elseif ( ! $has_avatar && $has_featured ) :
                             echo wp_get_attachment_image( $featured_id, 'large', false, ['class' => 'bw-img-real', 'style' => 'opacity:1;'] );

                        // 4. No Images
                        else : ?>
                            <div style="width:100%; height:100%; background:#ddd;"></div>
                        <?php endif; ?>
                    </div>
                    
                    <div class="bw-team-info">
                        <span class="bw-team-name"><?php echo esc_html($team_name); ?></span>
                        <?php if ( ! empty($job_title) ) : ?>
                            <span class="bw-team-job"><?php echo esc_html($job_title); ?></span>
                        <?php endif; ?>
                    </div>

                </a>

            <?php endwhile; ?>
        </div>

        <?php wp_reset_postdata(); ?>

    <?php else : ?>
        <p>No team members found.</p>
    <?php endif;

    return ob_get_clean();
}
add_shortcode( 'team_overlay', 'bw_team_overlay_shortcode' );