<?php

/**
 * Custom Blocks Registration
 * Laurent-Perrier Theme
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register ACF Blocks
 */
function lp_register_acf_blocks()
{
    /**
     * We register our block's with WordPress's handy
     * register_block_type();
     *
     * @link https://developer.wordpress.org/reference/functions/register_block_type/
     */

    register_block_type(__DIR__ . '/blocks/grillevideo');
    register_block_type(__DIR__ . '/blocks/cuvee');
    register_block_type(__DIR__ . '/blocks/liste');
    register_block_type(__DIR__ . '/blocks/cards-grid');
}
add_action('acf/init', 'lp_register_acf_blocks');

/**
 * Load custom Gutenberg blocks
 */
function lp_load_custom_blocks()
{
    $blocks_dir = get_template_directory() . '/includes/blocks/';

    // Array of block directories to load
    $block_directories = array(
        // 'table-of-contents',
        // Add more blocks here as needed
    );

    // Load each block
    foreach ($block_directories as $block_dir) {
        $block_file = $blocks_dir . $block_dir . '/' . $block_dir . '.php';
        if (file_exists($block_file)) {
            require_once $block_file;
        }
    }
}
add_action('init', 'lp_load_custom_blocks');

/**
 * Add block category for Laurent-Perrier blocks
 */
function lp_add_block_categories($categories)
{
    return array_merge(
        array(
            array(
                'slug'  => 'laurent-perrier',
                'title' => __('Laurent-Perrier', 'laurentperrier'),
                'icon'  => 'wine',
            ),
        ),
        $categories
    );
}
add_filter('block_categories_all', 'lp_add_block_categories');

/**
 * Enqueue block editor assets
 */
function lp_enqueue_block_editor_assets()
{
    wp_enqueue_style(
        'lp-block-editor-styles',
        get_template_directory_uri() . '/dist/admin.css',
        array(),
        wp_get_theme()->get('Version')
    );
}
add_action('enqueue_block_editor_assets', 'lp_enqueue_block_editor_assets');

/**
 * Dynamically populate ACF select choices for the Query loop block post_type field
 */
function lp_acf_load_post_type_choices($field)
{
    // Only target our specific field by key
    // Key defined in acf-json/group_cards_grid_block.json
    if (!isset($field['key']) || $field['key'] !== 'field_cg_post_type') {
        return $field;
    }

    $args = array(
        'public'   => true,
        '_builtin' => false,
    );

    // Include built-in post & page explicitly first
    $choices = array(
        'post' => __('Articles', 'laurentperrier'),
        'page' => __('Pages', 'laurentperrier'),
    );

    // Custom post types (public only)
    $custom_types = get_post_types($args, 'objects');
    foreach ($custom_types as $slug => $obj) {
        // Exclude unwanted
        if (in_array($slug, array('attachment', 'revision', 'nav_menu_item'), true)) {
            continue;
        }
        $choices[$slug] = $obj->labels->name ?: $slug;
    }

    // Also include built-in types that are public but not attachments
    $builtin_public = get_post_types(array('public' => true, '_builtin' => true), 'objects');
    foreach ($builtin_public as $slug => $obj) {
        if (in_array($slug, array('post', 'page', 'attachment'), true)) {
            continue;
        }
        $choices[$slug] = $obj->labels->name ?: $slug;
    }

    $field['choices'] = $choices;

    // Ensure default value is valid (ACF may provide default_value as string or array)
    $default = isset($field['default_value']) ? $field['default_value'] : '';
    if (is_array($default)) {
        $default = reset($default) ?: '';
    }

    if (empty($default) || !isset($choices[$default])) {
        $fallback = isset($choices['vins']) ? 'vins' : 'post';
        $field['default_value'] = $fallback;
    } else {
        $field['default_value'] = $default;
    }

    return $field;
}
add_filter('acf/load_field/key=field_cg_post_type', 'lp_acf_load_post_type_choices');
