<?php
/**
 * Field group « 📝 Article » (group_cvb_article) — champs En bref + Bibliographie
 * sur les articles de blog (post type natif `post`).
 *
 * Le corps de l'article est rédigé dans l'ÉDITEUR CLASSIQUE (post_content HTML,
 * pas de block builder / Gutenberg). En bref et Bibliographie sont deux champs
 * structurés dédiés, exposés en GraphQL :
 *   post.article { enBref { point } bibliographie { reference url } }
 */

defined('ABSPATH') || exit;

// Éditeur classique pour les articles de blog (désactive Gutenberg sur `post`).
add_filter('use_block_editor_for_post_type', static function ($use, $post_type) {
    return $post_type === 'post' ? false : $use;
}, 10, 2);

// Headless : aucun thème actif → activer explicitement l'image à la une.
add_action('after_setup_theme', static function () {
    add_theme_support('post-thumbnails');
});

add_action('acf/init', 'cvb_register_article_group');

function cvb_register_article_group(): void {
    if (!function_exists('acf_add_local_field_group')) {
        return;
    }

    $p = 'field_cvb_article';

    acf_add_local_field_group([
        'key'    => 'group_cvb_article',
        'title'  => '📝 Article',
        'fields' => [
            cvb_repeater("{$p}_en_bref", 'en_bref', 'En bref', [
                cvb_field("{$p}_en_bref_point", 'point', 'Point', 'textarea', ['rows' => 2]),
            ], ['button_label' => 'Ajouter un point']),
            cvb_repeater("{$p}_bibliographie", 'bibliographie', 'Bibliographie', [
                cvb_field("{$p}_bibliographie_reference", 'reference', 'Référence', 'textarea', [
                    'rows'         => 2,
                    'instructions' => 'Citation complète (style APA).',
                ]),
                cvb_field("{$p}_bibliographie_url", 'url', 'Lien', 'url'),
            ], ['button_label' => 'Ajouter une source']),
        ],
        'location' => [
            [['param' => 'post_type', 'operator' => '==', 'value' => 'post']],
        ],
        'position'           => 'normal',
        'menu_order'         => 5,
        'show_in_graphql'    => 1,
        'graphql_field_name' => 'article',
    ]);
}
