<?php

add_action( 'acf/init', 'cascapedia_st_jules_register_acf' );
function cascapedia_st_jules_register_acf(): void {
	if ( function_exists( 'acf_add_options_page' ) ) {
		acf_add_options_page([
			'page_title' => 'Site Settings',
			'menu_title' => 'Site Settings',
			'menu_slug'  => 'site-settings',
			'capability' => 'edit_posts',
			'redirect'   => false,
		]);
	}

	if ( ! function_exists( 'acf_add_local_field_group' ) ) return;

	acf_add_local_field_group([
		'key' => 'group_site_settings',
		'title' => 'Site Settings',
		'fields' => [
			csj_text('field_municipality_name', 'municipality_name', 'Nom municipal'),
			csj_text('field_municipality_short_name', 'municipality_short_name', 'Nom court'),
			csj_image('field_site_logo', 'site_logo', 'Logo'),
			csj_text('field_phone', 'phone', 'Téléphone'),
			csj_text('field_email', 'email', 'Courriel'),
			csj_textarea('field_address', 'address', 'Adresse'),
			csj_text('field_hours', 'hours', 'Heures'),
			csj_text('field_facebook_url', 'facebook_url', 'Facebook URL'),
		],
		'location' => [[['param' => 'options_page', 'operator' => '==', 'value' => 'site-settings']]],
	]);

	acf_add_local_field_group([
		'key' => 'group_page_content',
		'title' => 'Contenu de page',
		'fields' => [[
			'key' => 'field_sections',
			'label' => 'Sections',
			'name' => 'sections',
			'type' => 'flexible_content',
			'button_label' => 'Ajouter une section',
			'layouts' => cascapedia_st_jules_acf_layouts(),
		]],
		'location' => [[['param' => 'post_type', 'operator' => '==', 'value' => 'page']]],
	]);

	acf_add_local_field_group([
		'key' => 'group_document_fields',
		'title' => 'Document',
		'fields' => [ csj_file('field_document_file', 'document_file', 'PDF'), csj_date('field_document_date', 'document_date', 'Date') ],
		'location' => [[['param' => 'post_type', 'operator' => '==', 'value' => 'document']]],
	]);
	acf_add_local_field_group([
		'key' => 'group_event_fields',
		'title' => 'Événement',
		'fields' => [ csj_date('field_event_date', 'event_date', 'Date'), csj_text('field_event_location', 'location', 'Lieu') ],
		'location' => [[['param' => 'post_type', 'operator' => '==', 'value' => 'event']]],
	]);
	acf_add_local_field_group([
		'key' => 'group_business_fields',
		'title' => 'Entreprise',
		'fields' => [ csj_text('field_business_phone', 'phone', 'Téléphone'), csj_text('field_business_email', 'email', 'Courriel'), csj_text('field_business_website', 'website', 'Site web URL'), csj_textarea('field_business_address', 'address', 'Adresse') ],
		'location' => [[['param' => 'post_type', 'operator' => '==', 'value' => 'local_business']]],
	]);
	acf_add_local_field_group([
		'key' => 'group_gallery_fields',
		'title' => 'Galerie',
		'fields' => [ csj_text('field_gallery_key', 'gallery_key', 'Clé'), csj_gallery('field_gallery_photos', 'gallery_photos', 'Photos') ],
		'location' => [[['param' => 'post_type', 'operator' => '==', 'value' => 'gallery']]],
	]);
}

add_filter( 'acf/settings/save_json', fn() => get_stylesheet_directory() . '/acf-json' );
add_filter( 'acf/settings/load_json', function( array $paths ): array { $paths[] = get_stylesheet_directory() . '/acf-json'; return $paths; });

function cascapedia_st_jules_acf_layouts(): array {
	return [
		csj_layout('hero_carousel', 'Hero carousel', [ csj_number('field_hero_autoplay', 'autoplay_ms', 'Autoplay ms'), csj_repeater('field_hero_slides', 'slides', 'Slides', [ csj_image('field_hero_slide_image', 'image', 'Image'), csj_text('field_hero_slide_eyebrow', 'eyebrow', 'Surtitre'), csj_text('field_hero_slide_title', 'title', 'Titre'), csj_text('field_hero_slide_subtitle', 'subtitle', 'Sous-titre') ]) ]),
		csj_layout('text_intro', 'Texte intro', [ csj_text('field_text_intro_eyebrow', 'eyebrow', 'Surtitre'), csj_text('field_text_intro_title', 'title', 'Titre'), csj_wysiwyg('field_text_intro_body', 'body', 'Texte'), csj_select('field_text_intro_align', 'align', 'Alignement', [ 'center' => 'Centre', 'left' => 'Gauche' ]) ]),
		csj_layout('text_image', 'Texte + image', [ csj_text('field_text_image_title', 'title', 'Titre'), csj_wysiwyg('field_text_image_body', 'body', 'Texte'), csj_image('field_text_image_image', 'image', 'Image'), csj_select('field_text_image_position', 'image_position', 'Position image', [ 'right' => 'Droite', 'left' => 'Gauche' ]), ...csj_cta_fields('field_text_image') ]),
		csj_layout('services_list', 'Liste de services', [ csj_text('field_services_title', 'title', 'Titre'), csj_textarea('field_services_intro', 'intro', 'Intro'), csj_repeater('field_services_items', 'services', 'Services', [ csj_text('field_service_title', 'title', 'Titre'), csj_textarea('field_service_description', 'description', 'Description'), csj_textarea('field_service_items', 'items', 'Items (un par ligne)') ]) ]),
		csj_layout('photo_gallery', 'Galerie photos', [ csj_text('field_photo_gallery_title', 'title', 'Titre'), csj_textarea('field_photo_gallery_intro', 'intro', 'Intro'), csj_gallery('field_photo_gallery_photos', 'photos', 'Photos'), csj_text('field_photo_gallery_key', 'gallery_key', 'Clé galerie CPT') ]),
		csj_layout('events_list', 'Liste événements', [ csj_text('field_events_title', 'title', 'Titre'), csj_textarea('field_events_intro', 'intro', 'Intro') ]),
		csj_layout('businesses_list', 'Entreprises', [ csj_text('field_businesses_title', 'title', 'Titre'), csj_textarea('field_businesses_intro', 'intro', 'Intro') ]),
		csj_layout('cta_section', 'CTA', [ csj_text('field_cta_title', 'title', 'Titre'), csj_textarea('field_cta_subtitle', 'subtitle', 'Sous-titre'), csj_select('field_cta_background', 'background', 'Fond', [ 'primary' => 'Primaire', 'cream' => 'Crème' ]), ...csj_cta_fields('field_cta') ]),
		csj_layout('council_grid', 'Conseil municipal', [ csj_text('field_council_title', 'title', 'Titre'), csj_wysiwyg('field_council_intro', 'intro', 'Intro'), csj_repeater('field_council_members', 'members', 'Membres', [ csj_text('field_member_name', 'name', 'Nom'), csj_text('field_member_role', 'role', 'Rôle'), csj_text('field_member_email', 'email', 'Courriel'), csj_image('field_member_photo', 'photo', 'Photo') ]), csj_wysiwyg('field_council_footnote', 'footnote', 'Note') ]),
		csj_layout('documents_list', 'Liste documents', [ csj_text('field_documents_title', 'title', 'Titre'), csj_wysiwyg('field_documents_intro', 'intro', 'Intro') ]),
		csj_layout('pdf_embed', 'PDF intégré', [ csj_text('field_pdf_title', 'title', 'Titre'), csj_wysiwyg('field_pdf_intro', 'intro', 'Intro'), csj_file('field_pdf_file', 'file', 'PDF'), csj_number('field_pdf_height', 'height', 'Hauteur') ]),
	];
}

function csj_layout( string $name, string $label, array $sub_fields ): array { return [ 'key' => 'layout_' . $name, 'name' => $name, 'label' => $label, 'display' => 'block', 'sub_fields' => $sub_fields ]; }
function csj_text($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'text']; }
function csj_number($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'number']; }
function csj_date($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'date_picker','display_format'=>'Y-m-d','return_format'=>'Y-m-d','first_day'=>1]; }
function csj_textarea($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'textarea','rows'=>3]; }
function csj_wysiwyg($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'wysiwyg','tabs'=>'all','toolbar'=>'basic','media_upload'=>0]; }
function csj_image($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'image','return_format'=>'id','preview_size'=>'medium']; }
function csj_gallery($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'gallery','return_format'=>'id','preview_size'=>'medium']; }
function csj_file($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'file','return_format'=>'id','mime_types'=>'pdf']; }
function csj_select($key,$name,$label,array $choices): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'select','choices'=>$choices,'default_value'=>array_key_first($choices)]; }
function csj_true_false($key,$name,$label): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'true_false','ui'=>1]; }
function csj_repeater($key,$name,$label,array $sub_fields): array { return ['key'=>$key,'label'=>$label,'name'=>$name,'type'=>'repeater','layout'=>'row','button_label'=>'Ajouter','sub_fields'=>$sub_fields]; }
function csj_cta_fields(string $prefix): array { return [ csj_text($prefix.'_cta_label','cta_label','Libellé CTA'), csj_text($prefix.'_cta_url','cta_url','URL / courriel / téléphone'), csj_true_false($prefix.'_cta_external','cta_external','Lien externe') ]; }
