<?php
/**
 * Field group « Fiche lieu » (group_cvb_fiche_lieu) — données structurées
 * des pages locales hors flexible. GraphQL : lieu.ficheLieu { … }.
 */

defined('ABSPATH') || exit;

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

function cvb_lieu_reference_repeater(string $p, string $name, string $label): array {
    return cvb_repeater("{$p}_{$name}", $name, $label, [
        cvb_field("{$p}_{$name}_nom", 'nom', 'Nom', 'text'),
        cvb_field("{$p}_{$name}_slug", 'slug', 'Slug', 'text'),
        cvb_field("{$p}_{$name}_type_zone", 'type_zone', 'Type de zone', 'text'),
    ], ['layout' => 'table']);
}

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

    $p = 'field_cvb_fiche_lieu';

    acf_add_local_field_group([
        'key'    => 'group_cvb_fiche_lieu',
        'title'  => 'Fiche lieu',
        'fields' => [
            cvb_tab("{$p}_tab_identite", 'Identité'),
            cvb_select("{$p}_page_type", 'page_type', 'Type de page', [
                'ville'       => 'Ville',
                'departement' => 'Département',
                'region'      => 'Région',
            ], 'ville'),
            cvb_select("{$p}_pays", 'pays', 'Pays', [
                'FR' => 'France',
                'CH' => 'Suisse',
                'BE' => 'Belgique',
            ], 'FR'),
            cvb_field("{$p}_ville", 'ville', 'Ville', 'text'),
            cvb_field("{$p}_departement", 'departement', 'Département affiché', 'text'),
            cvb_field("{$p}_departement_nom", 'departement_nom', 'Nom du département', 'text'),
            cvb_field("{$p}_departement_num", 'departement_num', 'Numéro du département', 'text'),
            cvb_field("{$p}_region", 'region', 'Région', 'text'),
            cvb_field("{$p}_type_zone", 'type_zone', 'Type de zone', 'text'),
            cvb_field("{$p}_slug_ville", 'slug_ville', 'Slug ville', 'text'),
            cvb_field("{$p}_slug_departement", 'slug_departement', 'Slug département', 'text'),
            cvb_field("{$p}_slug_region", 'slug_region', 'Slug région', 'text'),
            cvb_field("{$p}_is_departement_page", 'is_departement_page', 'Page département', 'true_false', [
                'ui' => 1,
            ]),

            cvb_tab("{$p}_tab_medias", 'Médias'),
            cvb_image("{$p}_hero_image", 'hero_image', 'Image hero / OG', [
                'instructions' => 'Image utilisée dans le hero de la page lieu. Sert aussi d’image Open Graph si le champ SEO > Image OG est vide.',
            ]),
            cvb_field("{$p}_hero_image_credit", 'hero_image_credit', 'Crédit image hero', 'text', [
                'instructions' => 'Crédit/source libre de droit, utile si l’image vient d’une banque stock.',
            ]),
            cvb_field("{$p}_hero_image_source", 'hero_image_source', 'URL source image hero', 'url', [
                'instructions' => 'URL de la source/licence de l’image initiale.',
            ]),

            cvb_tab("{$p}_tab_maillage", 'Maillage'),
            cvb_lieu_reference_repeater($p, 'villes_departement', 'Villes du département'),
            cvb_repeater("{$p}_villes_proches", 'villes_proches', 'Villes proches', [
                cvb_field("{$p}_villes_proches_nom", 'nom', 'Nom', 'text'),
            ], ['layout' => 'table']),
            cvb_lieu_reference_repeater($p, 'maillage', 'Maillage interne'),
            cvb_select("{$p}_maillage_scope", 'maillage_scope', 'Portée du maillage', [
                'departement' => 'Département',
                'region'      => 'Région',
                'alentours'    => 'Alentours',
                'pays'         => 'Pays',
            ], null),

            cvb_tab("{$p}_tab_climat", 'Climat'),
            cvb_field("{$p}_climat_description", 'climat_description', 'Description climat courte', 'textarea', ['rows' => 3]),
            cvb_select("{$p}_zone_climatique", 'zone_climatique', 'Zone climatique', [
                'H1' => 'H1',
                'H2' => 'H2',
                'H3' => 'H3',
            ], 'H1'),
            cvb_select("{$p}_zone_climatique_cee", 'zone_climatique_cee', 'Zone climatique CEE', [
                'H1' => 'H1',
                'H2' => 'H2',
                'H3' => 'H3',
            ], 'H1'),
            cvb_field("{$p}_description_climatique", 'description_climatique', 'Description climatique longue', 'textarea', ['rows' => 5]),
            cvb_field("{$p}_jours_sup_30c", 'jours_sup_30c', 'Jours > 30 °C', 'number'),
            cvb_field("{$p}_temp_moy_estivale", 'temp_moy_estivale', 'Température moyenne estivale', 'number', ['step' => '0.1']),
            cvb_field("{$p}_pic_historique", 'pic_historique', 'Pic historique', 'number', ['step' => '0.1']),
            cvb_field("{$p}_jours_sup_30c_10ans", 'jours_sup_30c_10ans', 'Jours > 30 °C dans 10 ans', 'number'),
            cvb_field("{$p}_temp_moy_estivale_10ans", 'temp_moy_estivale_10ans', 'Température estivale dans 10 ans', 'number', ['step' => '0.1']),
            cvb_field("{$p}_jours_sup_30c_20ans", 'jours_sup_30c_20ans', 'Jours > 30 °C dans 20 ans', 'number'),
            cvb_field("{$p}_temp_moy_estivale_20ans", 'temp_moy_estivale_20ans', 'Température estivale dans 20 ans', 'number', ['step' => '0.1']),
            cvb_field("{$p}_reduction_sous_toiture", 'reduction_sous_toiture', 'Réduction sous toiture', 'number'),

            cvb_tab("{$p}_tab_contenu", 'Contenu local'),
            cvb_field("{$p}_intro_section", 'intro_section', 'Intro locale', 'group', [
                'layout'     => 'block',
                'sub_fields' => [
                    cvb_field("{$p}_intro_section_hook", 'hook', 'Hook', 'text'),
                    cvb_field("{$p}_intro_section_accroche", 'accroche', 'Accroche', 'textarea', ['rows' => 3]),
                    cvb_repeater("{$p}_intro_section_kpis", 'kpis', 'KPIs', [
                        cvb_field("{$p}_intro_section_kpis_value", 'value', 'Valeur', 'text'),
                        cvb_field("{$p}_intro_section_kpis_label", 'label', 'Label', 'text'),
                    ], ['layout' => 'table']),
                    cvb_field("{$p}_intro_section_contexte", 'contexte', 'Contexte', 'textarea', ['rows' => 3]),
                    cvb_field("{$p}_intro_section_cout_cache", 'cout_cache', 'Coût caché', 'textarea', ['rows' => 3]),
                    cvb_field("{$p}_intro_section_trust_line", 'trust_line', 'Trust line', 'text'),
                    cvb_field("{$p}_intro_section_urgence_line", 'urgence_line', 'Urgence line', 'text'),
                ],
            ]),
            cvb_field("{$p}_content", 'content', 'Contenu éditorial structuré', 'group', [
                'layout'     => 'block',
                'sub_fields' => [
                    cvb_field("{$p}_content_activites_intro", 'activites_intro', 'Intro activités', 'textarea', ['rows' => 4]),
                    cvb_repeater("{$p}_content_secteurs", 'secteurs', 'Secteurs', [
                        cvb_field("{$p}_content_secteurs_label", 'label', 'Label', 'text'),
                        cvb_image("{$p}_content_secteurs_image", 'image', 'Image'),
                        cvb_field("{$p}_content_secteurs_stat", 'stat', 'Stat', 'text'),
                        cvb_field("{$p}_content_secteurs_description", 'description', 'Description', 'textarea', ['rows' => 3]),
                    ]),
                    cvb_field("{$p}_content_probleme_title_part_1", 'probleme_title_part_1', 'Titre problème part 1', 'text'),
                    cvb_field("{$p}_content_probleme_title_part_2", 'probleme_title_part_2', 'Titre problème part 2', 'text'),
                    cvb_field("{$p}_content_probleme_paragraph", 'probleme_paragraph', 'Paragraphe problème', 'textarea', ['rows' => 4]),
                    cvb_field("{$p}_content_solution_title_part_1", 'solution_title_part_1', 'Titre solution part 1', 'text'),
                    cvb_field("{$p}_content_solution_title_part_2", 'solution_title_part_2', 'Titre solution part 2', 'text'),
                    cvb_field("{$p}_content_solution_paragraph", 'solution_paragraph', 'Paragraphe solution', 'textarea', ['rows' => 4]),
                    cvb_field("{$p}_content_preuve_big_stat_1", 'preuve_big_stat_1', 'Preuve stat 1', 'text'),
                    cvb_field("{$p}_content_preuve_big_stat_2", 'preuve_big_stat_2', 'Preuve stat 2', 'text'),
                    cvb_field("{$p}_content_preuve_paragraph", 'preuve_paragraph', 'Paragraphe preuve', 'textarea', ['rows' => 4]),
                ],
            ]),
            cvb_field("{$p}_seo_content", 'seo_content', 'Contenu SEO', 'group', [
                'layout'     => 'block',
                'sub_fields' => [
                    cvb_field("{$p}_seo_content_h2", 'h2', 'H2', 'text'),
                    cvb_field("{$p}_seo_content_intro", 'intro', 'Intro', 'textarea', ['rows' => 5]),
                    cvb_repeater("{$p}_seo_content_sections", 'sections', 'Sections', [
                        cvb_field("{$p}_seo_content_sections_h3", 'h3', 'H3', 'text'),
                        cvb_field("{$p}_seo_content_sections_content", 'content', 'Contenu', 'textarea', ['rows' => 5]),
                    ]),
                ],
            ]),
        ],
        'location' => [
            [['param' => 'post_type', 'operator' => '==', 'value' => 'lieu']],
        ],
        'position'           => 'normal',
        'menu_order'         => 1,
        'show_in_graphql'    => 1,
        'graphql_field_name' => 'ficheLieu',
    ]);
}
