/* 
Theme Name: Hello Elementor Child
Theme URI: https://github.com/elementor/hello-theme-child/
Description: Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author: Elementor Team
Author URI: https://elementor.com/
Template: hello-elementor
Version: 2.0.0
Text Domain: hello-elementor-child
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/* Add your custom styles here */


/*
add_action('admin_notices', function() {
    if (!current_user_can('manage_options')) return;

    // 1. Carga todos los attachments
    $attachments = get_posts([
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'fields' => 'ids',
    ]);
    $archivo_id_map = [];
    foreach ($attachments as $att_id) {
        $file = get_post_meta($att_id, '_wp_attached_file', true);
        $basename = strtolower(basename($file));
        $archivo_id_map[$basename] = $att_id;
    }

    // 2. Productos
    $productos = get_posts([
        'post_type' => 'product',
        'post_status' => ['publish','draft','pending','private'],
        'posts_per_page' => -1,
        'fields' => 'ids',
    ]);
    $extensiones = ['jpg', 'jpeg', 'png', 'webp'];

    echo '<div style="background: #fff; padding: 20px; border: 2px solid #333; max-height:600px; overflow:auto;"><pre>';
    echo "Asignando imágenes por SKU (imagen principal + galería)\n";
    foreach ($productos as $product_id) {
        $sku = get_post_meta($product_id, '_sku', true);
        if (!$sku) continue;

        $galeria_ids = [];
        $principal_id = null;

        foreach ($archivo_id_map as $basename => $att_id) {
            // Coincidencia: empieza con el SKU
            if (stripos($basename, strtolower($sku)) === 0) {
                // Extraer si termina con -1, -2, etc.
                if (preg_match('/^' . preg_quote(strtolower($sku), '/') . '[-_]?1\./', $basename)) {
                    $principal_id = $att_id;
                } else {
                    $galeria_ids[] = $att_id;
                }
            }
        }

        // Asignar imagen destacada y galería si hay coincidencias
        if ($principal_id) {
            set_post_thumbnail($product_id, $principal_id);
            update_post_meta($product_id, '_product_image_gallery', implode(',', $galeria_ids));
            echo "✅ $sku: Imagen principal (ID $principal_id), galería (IDs ".implode(',', $galeria_ids).")\n";
        } elseif (!empty($galeria_ids)) {
            // Si no hay -1, usa la primera como principal y las demás como galería
            set_post_thumbnail($product_id, $galeria_ids[0]);
            $rest = array_slice($galeria_ids, 1);
            update_post_meta($product_id, '_product_image_gallery', implode(',', $rest));
            echo "✅ $sku: Imagen principal (ID {$galeria_ids[0]}), galería (IDs ".implode(',', $rest).")\n";
        } else {
            echo "❌ $sku: Sin imágenes encontradas\n";
        }
    }
    echo "</pre></div>";
});
*/