<?php
/**
 * Cryzel Theme Functions
 *
 * This file contains the core functionality of the Cryzel theme.
 * It initializes the theme, loads required files, and sets up basic configurations.
 * The functions.php file serves as the entry point for the theme's PHP functionality.
 *
 * @package WordPress
 * @subpackage Cryzel
 * @since 1.0.0
 */

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

// Define theme version
if (!defined('CRYZEL_VERSION')) {
    define('CRYZEL_VERSION', wp_get_theme()->get('Version'));
}

// Define theme directory path
if (!defined('CRYZEL_DIR')) {
    define('CRYZEL_DIR', trailingslashit(get_template_directory()));
}

// Define theme directory URI
if (!defined('CRYZEL_URI')) {
    define('CRYZEL_URI', trailingslashit(get_template_directory_uri()));
}

/**
 * Load theme core functionality
 * 
 * The class-loader.php file contains the main theme class and autoloader.
 * This file initializes the theme's core functionality and sets up hooks.
 */
require_once CRYZEL_DIR . 'includes/class-loader.php';

/**
 * Register block patterns and block styles
 */
function cryzel_register_block_patterns() {
    // Register block patterns
    register_block_pattern_category('cryzel', [
        'label' => __('Cryzel', 'cryzel'),
    ]);
}
add_action('init', 'cryzel_register_block_patterns');

/**
 * Register block styles
 */
function cryzel_register_block_styles() {
    // Register block styles
    wp_register_style(
        'cryzel-block-styles',
        CRYZEL_URI . 'assets/css/blocks.css',
        [],
        CRYZEL_VERSION
    );
}
add_action('init', 'cryzel_register_block_styles');

/**
 * Enqueue theme assets
 */
function cryzel_enqueue_theme_scripts() {
    // Enqueue theme styles
    wp_enqueue_style('cryzel-style-index', get_theme_file_uri('build/style-index.css'), array(), CRYZEL_VERSION);

    // Add inline CSS for skip link as backup
    $skip_link_css = "
    .skip-link {
        position: absolute !important;
        top: -40px !important;
        left: 6px !important;
        background: #000 !important;
        color: #fff !important;
        padding: 8px 16px !important;
        text-decoration: none !important;
        border-radius: 4px !important;
        z-index: 10000 !important;
        transition: top 0.3s ease !important;
        font-weight: bold !important;
        display: block !important;
        width: auto !important;
        height: auto !important;
        overflow: visible !important;
    }
    .skip-link:focus {
        top: 42px !important;
        outline: 2px solid #fff !important;
        outline-offset: 2px !important;
    }
    ";
    wp_add_inline_style('cryzel-style-index', $skip_link_css);
}
add_action('wp_enqueue_scripts', 'cryzel_enqueue_theme_scripts');

// Add skip link for accessibility at the very top of the body
add_action('wp_body_open', function() {
    echo '<a href="#main-content" class="skip-link screen-reader-text" id="skip-link">' . esc_html__('Skip to content', 'cryzel') . '</a>';
});

// Initialize theme features and editor styles
function cryzel_theme_setup() {
    // Add theme support for editor styles
    add_theme_support('editor-styles');
    
    // Add editor style
    add_editor_style('build/style-index.css');
}
add_action('after_setup_theme', 'cryzel_theme_setup');

// Enqueue editor styles
function cryzel_enqueue_editor_assets() {
    $style_css = 'build/style-index.css';
    $style_path = get_template_directory() . '/' . $style_css;
    
    if (file_exists($style_path)) {
        wp_enqueue_style(
            'cryzel-editor-style',
            get_template_directory_uri() . '/' . $style_css,
            array('wp-edit-blocks'),
            filemtime($style_path)
        );
    }
}
add_action('enqueue_block_editor_assets', 'cryzel_enqueue_editor_assets');

/**
 * Initialize the theme
 * 
 * Creates a singleton instance of the main theme class.
 * This ensures the theme is properly initialized with all required functionality.
 */
if (class_exists('Cryzel_Theme')) {
    Cryzel_Theme::get_instance();
}
