<?php
/**
 * Crowdfunding Platform
 */

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

/**
 * Theme setup.
 */
if ( ! function_exists( 'crowdfunding_platform_setup' ) ) :
function crowdfunding_platform_setup() {
    // Make theme available for translation.
    load_theme_textdomain( 'crowdfunding-platform', get_template_directory() . '/languages' );
    
    // Add theme support for various WordPress features
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'title-tag' );
    add_theme_support( 'html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script'
    ) );
    
    // Enable support for post formats (required for WordPress.org)
    add_theme_support( 'post-formats', array(
        'aside',
        'image',
        'video',
        'quote',
        'link',
        'gallery',
        'audio',
        'status',
        'chat'
    ) );
    
    // Add support for block editor features
    add_theme_support( 'wp-block-styles' );
    add_theme_support( 'responsive-embeds' );
    add_theme_support( 'align-wide' );
}
endif;
add_action( 'after_setup_theme', 'crowdfunding_platform_setup' );

/**
 * Enqueue styles and scripts.
 */
if ( ! function_exists( 'crowdfunding_platform_scripts' ) ) :
function crowdfunding_platform_scripts() {
    // Enqueue theme stylesheet
    wp_enqueue_style( 
        'crowdfunding-platform-style', 
        get_parent_theme_file_uri( 'style.css' ),
        array(),
        wp_get_theme()->get( 'Version' )
    );
    
    // Enqueue theme JavaScript
    wp_enqueue_script(
        'crowdfunding-platform-script',
        get_parent_theme_file_uri( 'assets/js/theme.js' ),
        array(),
        wp_get_theme()->get( 'Version' ),
        true
    );
}
endif;
add_action( 'wp_enqueue_scripts', 'crowdfunding_platform_scripts' );

/**
 * Register pattern categories.
 */
if ( ! function_exists( 'crowdfunding_platform_pattern_categories' ) ) :
function crowdfunding_platform_pattern_categories() {
    register_block_pattern_category(
        'crowdfunding_platform',
        array(
            'label'       => __( 'Crowdfunding Platform', 'crowdfunding-platform' ),
            'description' => __( 'A collection of patterns for crowdfunding websites.', 'crowdfunding-platform' ),
        )
    );

    // Register page patterns category (following TwentyTwentyFive approach)
    register_block_pattern_category(
        'crowdfunding_platform_page',
        array(
            'label'       => __( 'Pages', 'crowdfunding-platform' ),
            'description' => __( 'Complete page layouts and starter content.', 'crowdfunding-platform' ),
        )
    );
}
endif;
add_action( 'init', 'crowdfunding_platform_pattern_categories' );

/**
 * Allow plugins to register patterns with the theme.
 */
if ( ! function_exists( 'crowdfunding_platform_register_plugin_patterns' ) ) :
function crowdfunding_platform_register_plugin_patterns() {
    /**
     * Filter to allow plugins to register patterns
     * 
     * @param array $patterns Array of pattern definitions
     * @return array Modified array of patterns
     * 
     * Example usage:
     * add_filter( 'crowdfunding_platform_register_patterns', function( $patterns ) {
     *     $patterns[] = array(
     *         'slug'        => 'my-plugin/pattern-name',
     *         'title'       => 'Pattern Title',
     *         'content'     => '<!-- wp:paragraph --><p>Content</p><!-- /wp:paragraph -->',
     *         'description' => 'Optional description',
     *         'categories'  => array( 'crowdfunding_platform' ),
     *     );
     *     return $patterns;
     * });
     */
    $plugin_patterns = apply_filters( 'crowdfunding_platform_register_patterns', array() );
    
    // Validate and register each pattern
    if ( ! is_array( $plugin_patterns ) ) {
        return;
    }
    
    foreach ( $plugin_patterns as $pattern ) {
        // Skip invalid patterns
        if ( ! crowdfunding_platform_validate_pattern( $pattern ) ) {
            continue;
        }
        
        // Skip if pattern already exists
        $registry = WP_Block_Patterns_Registry::get_instance();
        if ( $registry->is_registered( $pattern['slug'] ) ) {
            continue;
        }
        
        // Handle categories - preserve string categories by converting to array
        if ( ! isset( $pattern['categories'] ) ) {
            $pattern['categories'] = array();
        } elseif ( is_string( $pattern['categories'] ) ) {
            $pattern['categories'] = array( $pattern['categories'] );
        } elseif ( ! is_array( $pattern['categories'] ) ) {
            $pattern['categories'] = array();
        }
        
        // Auto-add theme category if not present
        if ( ! in_array( 'crowdfunding_platform', $pattern['categories'], true ) ) {
            $pattern['categories'][] = 'crowdfunding_platform';
        }
        
        // Set source to plugin if not specified
        if ( ! isset( $pattern['source'] ) ) {
            $pattern['source'] = 'plugin';
        }
        
        // Register the pattern with WordPress
        register_block_pattern(
            $pattern['slug'],
            $pattern
        );
    }
}
endif;
add_action( 'init', 'crowdfunding_platform_register_plugin_patterns', 11 );

/**
 * Validate a pattern definition.
 * 
 * @param mixed $pattern Pattern definition to validate
 * @return bool True if valid, false otherwise
 */
if ( ! function_exists( 'crowdfunding_platform_validate_pattern' ) ) :
function crowdfunding_platform_validate_pattern( $pattern ) {
    // Must be an array
    if ( ! is_array( $pattern ) ) {
        return false;
    }
    
    // Check required fields
    $required_fields = array( 'slug', 'title', 'content' );
    foreach ( $required_fields as $field ) {
        if ( empty( $pattern[ $field ] ) || ! is_string( $pattern[ $field ] ) ) {
            return false;
        }
    }
    
    // Validate slug format (must contain forward slash for namespace)
    if ( strpos( $pattern['slug'], '/' ) === false ) {
        return false;
    }
    
    // Validate slug doesn't use theme namespace
    if ( strpos( $pattern['slug'], 'crowdfunding-platform/' ) === 0 ) {
        return false;
    }
    
    return true;
}
endif;

/**
 * Load development functions when CROWDFUNDING_PLATFORM_DEV is enabled.
 */
if ( defined( 'CROWDFUNDING_PLATFORM_DEV' ) && CROWDFUNDING_PLATFORM_DEV ) {
    $dev_functions = get_template_directory() . '/functions-dev.php';
    if ( file_exists( $dev_functions ) ) {
        require_once $dev_functions;
    }
}




/**
 * Register block style variations.
 */
if ( ! function_exists( 'crowdfunding_platform_register_block_styles' ) ) :
function crowdfunding_platform_register_block_styles() {
    // Note: Button outline style is already provided by WordPress Core
    // Our CSS in style.css customizes the core-provided outline style
    
    // Group - Card style
    register_block_style('core/group', array(
        'name' => 'crowdfunding-platform-card',
        'label' => __('Card', 'crowdfunding-platform')
    ));

    // Quote - Testimonial style
    register_block_style('core/quote', array(
        'name' => 'crowdfunding-platform-testimonial',
        'label' => __('Testimonial', 'crowdfunding-platform')
    ));
}
endif;
add_action('init', 'crowdfunding_platform_register_block_styles');
