<?php
/**
 * set_search_template.php (#8) — instala un template FSE 'search' con resultados
 * COMPACTOS (rejilla de tarjetas título+fecha+extracto), igual que el 'archive'
 * (#63), en vez del patrón genérico del tema que muestra los posts «todos seguidos».
 *
 * Reutiliza las clases fea-archive-grid / fea-archive-card (mismo CSS ya presente).
 * Idempotente: crea el wp_template 'search' si no existe, o actualiza su contenido.
 *
 * Uso (dentro del contenedor / wp-cli):
 *   wp eval-file scripts/set_search_template.php            # DRY-RUN
 *   APPLY=1 wp eval-file scripts/set_search_template.php    # aplica
 */
$apply = getenv('APPLY') === '1';
$theme = get_stylesheet(); // twentytwentyfive

$content = <<<HTML
<!-- wp:template-part {"slug":"header","theme":"{$theme}"} /-->

<!-- wp:group {"tagName":"main","style":{"spacing":{"margin":{"top":"var:preset|spacing|60"}}},"layout":{"type":"constrained"}} -->
<main class="wp-block-group" style="margin-top:var(--wp--preset--spacing--60)"><!-- wp:query-title {"type":"search","align":"wide","fontSize":"x-large"} /-->

<!-- wp:spacer {"height":"var:preset|spacing|40"} -->
<div style="height:var(--wp--preset--spacing--40)" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:query {"queryId":74,"query":{"perPage":12,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true,"taxQuery":null,"parents":[]},"align":"wide","layout":{"type":"default"}} -->
<div class="wp-block-query alignwide"><!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:query-no-results {"align":"wide","fontSize":"medium"} -->
<!-- wp:paragraph -->
<p>Lo siento, no se ha encontrado nada. Por favor, prueba a buscar con otras palabras clave.</p>
<!-- /wp:paragraph -->
<!-- /wp:query-no-results --></div>
<!-- /wp:group -->

<!-- wp:post-template {"align":"wide","className":"fea-archive-grid","style":{"spacing":{"blockGap":"1.5rem"}},"layout":{"type":"grid","columnCount":3}} -->
<!-- wp:group {"className":"fea-archive-card","layout":{"type":"constrained"}} -->
<div class="wp-block-group fea-archive-card"><!-- wp:post-title {"isLink":true,"className":"fea-archive-title","fontSize":"medium"} /-->

<!-- wp:post-date {"isLink":true,"className":"fea-archive-date","fontSize":"small"} /-->

<!-- wp:post-excerpt {"showMoreOnNewLine":false,"excerptLength":22,"className":"fea-archive-excerpt"} /--></div>
<!-- /wp:group -->
<!-- /wp:post-template -->

<!-- wp:spacer {"height":"var:preset|spacing|30"} -->
<div style="height:var(--wp--preset--spacing--30)" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:group {"align":"full","style":{"spacing":{"margin":{"top":"var:preset|spacing|40","bottom":"var:preset|spacing|40"}}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group alignfull" style="margin-top:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--40)"><!-- wp:query-pagination {"align":"full","style":{"typography":{"fontStyle":"normal","fontWeight":"400"}},"layout":{"type":"flex","justifyContent":"space-between","flexWrap":"wrap"}} -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination --></div>
<!-- /wp:group --></div>
<!-- /wp:query --></main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer","theme":"{$theme}"} /-->
HTML;

$existing = get_posts(['post_type' => 'wp_template', 'name' => 'search', 'post_status' => 'any', 'posts_per_page' => 1]);
if ($existing) {
    $id = $existing[0]->ID;
    echo "Template 'search' existe (post $id) → " . ($apply ? "actualizando" : "[dry] actualizaría") . "\n";
    if ($apply) wp_update_post(['ID' => $id, 'post_content' => $content]);
} else {
    echo ($apply ? "Creando" : "[dry] crearía") . " wp_template 'search' (theme $theme)\n";
    if ($apply) {
        $id = wp_insert_post([
            'post_type'    => 'wp_template',
            'post_status'  => 'publish',
            'post_name'    => 'search',
            'post_title'   => 'Search Results',
            'post_content' => $content,
        ], true);
        if (is_wp_error($id)) { echo "ERROR: " . $id->get_error_message() . "\n"; return; }
        wp_set_object_terms($id, $theme, 'wp_theme');
        echo "  creado post $id\n";
    }
}
if (function_exists('wp_cache_flush')) wp_cache_flush();
echo ($apply ? "APLICADO" : "DRY-RUN") . "\n";
