<?php
/**
 * Field group « Fiche référence » (group_cvb_fiche_reference) — données
 * structurées du chantier hors flexible. GraphQL : reference.ficheReference { … }.
 */

defined('ABSPATH') || exit;

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

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

    $p = 'field_cvb_fiche_reference';

    acf_add_local_field_group([
        'key'    => 'group_cvb_fiche_reference',
        'title'  => 'Fiche référence',
        'fields' => [
            cvb_field("{$p}_client_name", 'client_name', 'Nom du client', 'text'),
            cvb_field("{$p}_location", 'location', 'Localisation', 'text'),
            cvb_field("{$p}_produit", 'produit', 'Produit appliqué', 'relationship', [
                'post_type'     => ['produit'],
                'max'           => 1,
                'return_format' => 'object',
                'filters'       => ['search'],
            ]),
            cvb_field("{$p}_support", 'support', 'Support', 'text'),
            cvb_field("{$p}_surface", 'surface', 'Surface', 'text'),
            cvb_repeater("{$p}_metrics", 'metrics', 'Métriques', [
                cvb_field("{$p}_metrics_value", 'value', 'Valeur', 'text'),
                cvb_field("{$p}_metrics_label", 'label', 'Label', 'text'),
            ], ['layout' => 'table']),
            cvb_field("{$p}_quote", 'quote', 'Citation', 'group', [
                'layout'     => 'block',
                'sub_fields' => [
                    cvb_field("{$p}_quote_texte", 'texte', 'Texte', 'textarea', ['rows' => 3]),
                    cvb_field("{$p}_quote_auteur", 'auteur', 'Auteur', 'text'),
                    cvb_field("{$p}_quote_role", 'role', 'Rôle', 'text'),
                ],
            ]),
            cvb_field("{$p}_video_url", 'video_url', 'URL vidéo', 'url'),
            cvb_field("{$p}_show_in_grid", 'show_in_grid', 'Afficher dans les grilles', 'true_false', [
                'ui'            => 1,
                'default_value' => 1,
            ]),
        ],
        'location' => [
            [['param' => 'post_type', 'operator' => '==', 'value' => 'reference']],
        ],
        'position'           => 'normal',
        'menu_order'         => 1,
        'show_in_graphql'    => 1,
        'graphql_field_name' => 'ficheReference',
    ]);
}
