<?php
/**
 * Block Support and Gutenberg Integration
 *
 * This file contains all block-related functionality including:
 * - Block styles registration
 * - Block editor assets
 * - Gutenberg theme support
 * - Custom block configurations
 *
 * @package Blog Eye
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Add Gutenberg and block editor support to theme
 */
function blog_eye_add_block_support() {
    // Add support for Block Styles.
    add_theme_support('wp-block-styles');

    // Add support for full and wide align images.
    add_theme_support('align-wide');
    add_theme_support("responsive-embeds");

    // Add editor styles support
    add_theme_support('editor-styles');
    add_editor_style('assets/css/admin-block.css');

    // Add support for custom line height controls
    add_theme_support('custom-line-height');

    // Add support for custom units
    add_theme_support('custom-units');

    // Add support for custom spacing controls
    add_theme_support('custom-spacing');

    // Add theme support for block color palette
    add_theme_support('editor-color-palette', array(
        array(
            'name'  => esc_html__('Strong Blue', 'blog-eye'),
            'slug'  => 'strong-blue',
            'color' => '#0073aa',
        ),
        array(
            'name'  => esc_html__('Lighter Blue', 'blog-eye'),
            'slug'  => 'lighter-blue',
            'color' => '#229fd8',
        ),
        array(
            'name'  => esc_html__('Very Dark Gray', 'blog-eye'),
            'slug'  => 'very-dark-gray',
            'color' => '#444444',
        ),
        array(
            'name'  => esc_html__('Very Light Gray', 'blog-eye'),
            'slug'  => 'very-light-gray',
            'color' => '#eeeeee',
        ),
        array(
            'name'  => esc_html__('White', 'blog-eye'),
            'slug'  => 'white',
            'color' => '#ffffff',
        ),
        array(
            'name'  => esc_html__('Black', 'blog-eye'),
            'slug'  => 'black',
            'color' => '#000000',
        ),
    ));

    // Add theme support for font sizes
    add_theme_support('editor-font-sizes', array(
        array(
            'name' => esc_html__('Small', 'blog-eye'),
            'size' => 14,
            'slug' => 'small'
        ),
        array(
            'name' => esc_html__('Normal', 'blog-eye'),
            'size' => 17,
            'slug' => 'normal'
        ),
        array(
            'name' => esc_html__('Medium', 'blog-eye'),
            'size' => 18,
            'slug' => 'medium'
        ),
        array(
            'name' => esc_html__('Large', 'blog-eye'),
            'size' => 24,
            'slug' => 'large'
        ),
        array(
            'name' => esc_html__('Larger', 'blog-eye'),
            'size' => 32,
            'slug' => 'larger'
        )
    ));
}
add_action('after_setup_theme', 'blog_eye_add_block_support');

/**
 * Register custom block styles
 * 
 * This function registers block styles as recommended for transitioning to block themes
 */
function blog_eye_register_block_styles() {
    // Button block styles
    register_block_style(
        'core/button',
        array(
            'name'         => 'outline',
            'label'        => __('Outline', 'blog-eye'),
        )
    );

    register_block_style(
        'core/button',
        array(
            'name'         => 'squared',
            'label'        => __('Squared', 'blog-eye'),
        )
    );

    // Quote block styles
    register_block_style(
        'core/quote',
        array(
            'name'         => 'fancy-quote',
            'label'        => __('Fancy Quote', 'blog-eye'),
        )
    );

    // Image block styles
    register_block_style(
        'core/image',
        array(
            'name'         => 'rounded',
            'label'        => __('Rounded', 'blog-eye'),
        )
    );

    // Cover block styles
    register_block_style(
        'core/cover',
        array(
            'name'         => 'blur-overlay',
            'label'        => __('Blur Overlay', 'blog-eye'),
        )
    );

    // Group block styles
    register_block_style(
        'core/group',
        array(
            'name'         => 'shadow-box',
            'label'        => __('Shadow Box', 'blog-eye'),
        )
    );

    // List block styles
    register_block_style(
        'core/list',
        array(
            'name'         => 'no-bullets',
            'label'        => __('No Bullets', 'blog-eye'),
        )
    );

    register_block_style(
        'core/list',
        array(
            'name'         => 'checkmark-list',
            'label'        => __('Checkmark List', 'blog-eye'),
        )
    );

    // Separator block styles
    register_block_style(
        'core/separator',
        array(
            'name'         => 'wide',
            'label'        => __('Wide Line', 'blog-eye'),
        )
    );

    register_block_style(
        'core/separator',
        array(
            'name'         => 'dots',
            'label'        => __('Three Dots', 'blog-eye'),
        )
    );

    // Table block styles
    register_block_style(
        'core/table',
        array(
            'name'         => 'stripes',
            'label'        => __('Stripes', 'blog-eye'),
        )
    );

    // Social Links block styles
    register_block_style(
        'core/social-links',
        array(
            'name'         => 'logos-only',
            'label'        => __('Logos Only', 'blog-eye'),
        )
    );
}
add_action('init', 'blog_eye_register_block_styles');

/**
 * Enqueue block editor styles (backend only)
 */
function blog_eye_block_editor_assets() {
    wp_enqueue_style(
        'blog-eye-block-editor-styles',
        get_template_directory_uri() . '/assets/css/admin-block.css',
        array(),
        BLOG_EYE_VERSION
    );
}
add_action('enqueue_block_editor_assets', 'blog_eye_block_editor_assets');

/**
 * Enqueue block styles for both frontend and backend
 */
function blog_eye_block_assets() {
    wp_enqueue_style(
        'blog-eye-blocks-style',
        get_template_directory_uri() . '/assets/css/block.css',
        array(),
        BLOG_EYE_VERSION
    );
}
add_action('enqueue_block_assets', 'blog_eye_block_assets');

/**
 * Add custom Gutenberg editor configuration
 */
function blog_eye_gutenberg_css() {
    // Enable wide alignment support
    add_theme_support('align-wide');
    
    // Let users customize colors and fonts (remove restrictions for better user experience)
    // add_theme_support('disable-custom-colors');
    // add_theme_support('disable-custom-font-sizes');
}
add_action('after_setup_theme', 'blog_eye_gutenberg_css');
