<?php
/**
 * Template Meta Settings
 *
 * Registers meta fields for page templates (Blog Grid, etc.)
 * These settings appear in the block editor sidebar when the template is selected.
 *
 * @package Blog_Over
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register meta fields for page templates
 */
function blog_over_register_template_meta()
{
    $meta_fields = array(
        '_blog_over_template_sidebar' => array(
            'type' => 'string',
            'default' => 'right-sidebar',
        ),
        '_blog_over_template_blog_style' => array(
            'type' => 'string',
            'default' => 'default',
        ),
        '_blog_over_template_grid_columns' => array(
            'type' => 'integer',
            'default' => 3,
        ),
        '_blog_over_template_posts_per_page' => array(
            'type' => 'integer',
            'default' => 9,
        ),
        '_blog_over_template_categories' => array(
            'type' => 'string',
            'default' => '',
        ),
        '_blog_over_template_orderby' => array(
            'type' => 'string',
            'default' => 'date',
        ),
        '_blog_over_template_order' => array(
            'type' => 'string',
            'default' => 'DESC',
        ),
        '_blog_over_template_show_pagination' => array(
            'type' => 'string',
            'default' => '1',
        ),
        '_blog_over_template_section_width' => array(
            'type' => 'string',
            'default' => 'boxed',
        ),
    );

    foreach ($meta_fields as $meta_key => $config) {
        register_post_meta('page', $meta_key, array(
            'show_in_rest' => true,
            'single' => true,
            'type' => $config['type'],
            'default' => $config['default'],
            'auth_callback' => function () {
                return current_user_can('edit_posts');
            },
        ));
    }
}
add_action('init', 'blog_over_register_template_meta');

/**
 * Enqueue block editor assets for template settings
 */
function blog_over_enqueue_template_editor_assets()
{
    $screen = get_current_screen();

    // Only load on page editor
    if (!$screen || $screen->post_type !== 'page') {
        return;
    }

    // Enqueue the settings panel script
    wp_enqueue_script(
        'blog-over-template-settings',
        BLOG_OVER_URI . '/assets/js/blog-template-settings.js',
        array('wp-plugins', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-components', 'wp-data', 'wp-compose', 'wp-i18n'),
        BLOG_OVER_VERSION,
        true
    );

    // Pass categories to JS
    $categories = get_categories(array('hide_empty' => false));
    $cat_options = array();
    foreach ($categories as $cat) {
        $cat_options[] = array(
            'value' => $cat->term_id,
            'label' => $cat->name,
        );
    }

    wp_localize_script('blog-over-template-settings', 'novaTemplateData', array(
        'categories' => $cat_options,
        'templates' => array('template-blog-custom.php', 'template-page-layout.php'),
    ));

    // Editor styles
    wp_enqueue_style(
        'blog-over-template-editor',
        BLOG_OVER_URI . '/assets/css/blog-template-editor.css',
        array(),
        BLOG_OVER_VERSION
    );
}
add_action('enqueue_block_editor_assets', 'blog_over_enqueue_template_editor_assets');
