<?php
/**
 * Plantilla custom para Relevanssi Live Ajax Search (grid).
 * NO header/footer. Solo imprime el grid de resultados.
 */
if ( ! defined('ABSPATH') ) exit;

/* Solo si es una llamada AJAX del live-search */
if ( empty($_REQUEST['action']) || $_REQUEST['action'] !== 'relevanssi_live_search' ) {
    return; // no renderices nada para otras situaciones
}

global $relevanssi_query;
$q = ( isset($relevanssi_query) && $relevanssi_query instanceof WP_Query ) ? $relevanssi_query : null;
$posts = $q ? $q->posts : array();



/**
 * Devuelve una URL de imagen “mejor esfuerzo” según el tipo de post:
 * - artist-video  → ACF cover_image
 * - activity      → ACF featured_image
 * - profile       → ACF portrait | portait
 * - cualquier post/page → thumbnail
 * + fallbacks de tamaños y placeholder.
 */
function rlvls_result_image_url( $pid, $ptype ) {
    // 1) Según post type y sus ACF habituales
    if ( $ptype === 'artist-video' ) {
        $img = get_field('cover_image', $pid);
        if ( is_array($img) ) {
            return $img['sizes']['large'] ?? $img['sizes']['medium_large'] ?? $img['url'] ?? '';
        }
    }
    if ( $ptype === 'activity' || $ptype === 'venue' ) {
        $img = get_field('featured_image', $pid);
        if ( is_array($img) ) {
            return $img['sizes']['large'] ?? $img['sizes']['medium_large'] ?? $img['url'] ?? '';
        }
    }
    if ( $ptype === 'profile' ) {
        $att_id = get_field('portrait', $pid, false);
        if ( ! $att_id ) $att_id = get_field('portait', $pid, false); // typo legacy
        if ( $att_id ) {
            $src = wp_get_attachment_image_src( (int)$att_id, 'large' );
            if ( ! empty($src[0]) ) return $src[0];
        }
    }

    // 2) Imagen destacada nativa
    $thumb = get_the_post_thumbnail_url( $pid, 'large' );
    if ( $thumb ) return $thumb;

    // 3) Último recurso: placeholder del tema
    return get_template_directory_uri() . '/img/horizontal-placeholder.svg';
}
?>
<div class="archive-grid search-grid">
  <?php if ( ! empty( $posts ) ) : ?>
    <?php
    global $post; $old_post = $post;
    foreach ( $posts as $p ) :
        $post   = $p; setup_postdata( $post );
        $pid    = (int) $p->ID;
        $ptype  = get_post_type( $pid );
        $title  = get_the_title( $pid );
        $url    = get_permalink( $pid );
        $thumb  = rlvls_result_image_url( $pid, $ptype );

        $pt_obj = get_post_type_object( $ptype );
        $label  = ( $pt_obj && ! empty( $pt_obj->labels->singular_name ) ) ? $pt_obj->labels->singular_name : '';
    ?>
      <div class="archive-item">
        <a class="archive-new-thumb" href="<?php echo esc_url( $url ); ?>">
          <div class="archive-thumb__image-container search">
            <img src="<?php echo esc_url( $thumb ); ?>" alt="<?php echo esc_attr( $title ); ?>">
          </div>
          <div class="archive-thumb__text">
            <?php if ( $label ) : ?>
              <div class="archive-thumb__label"><?php echo esc_html( $label ); ?></div>
            <?php endif; ?>
            <h3 class="archive-thumb__title"><?php echo esc_html( $title ); ?></h3>
          </div>
        </a>
      </div>
    <?php endforeach; wp_reset_postdata(); $post = $old_post; ?>
  <?php else : ?>
    <p class="eta u_mt-2"><?php esc_html_e( 'No results found.', 'far_loop' ); ?></p>
  <?php endif; ?>
</div>