<?php 

/**
 * Registers block patterns and categories for the Cuppa theme.
 *
 * @since 1.0
 */
function cuppa_register_block_patterns() {
    $default_categories = array(
        'cuppa' => array(
            'label' => esc_html__('Cuppa', 'cuppa'),
            'description' => esc_html__('Patterns designed specifically for the Cuppa theme', 'cuppa'),
        ),
        'cuppa-pages' => array(
            'label' => esc_html__('Pages', 'cuppa'),
            'description' => esc_html__('Full page layouts', 'cuppa'),
        ),
        'cuppa-sections' => array(
            'label' => esc_html__('Sections', 'cuppa'),
            'description' => esc_html__('Reusable content sections', 'cuppa'),
        ),
    );  
    $block_pattern_categories = apply_filters('cuppa_block_pattern_categories', $default_categories);

    foreach ($block_pattern_categories as $name => $properties) {
        if (!WP_Block_Pattern_Categories_Registry::get_instance()->is_registered($name)) {
            register_block_pattern_category($name, $properties);
        }
    }
    $patterns_directory = get_template_directory() . '/patterns/';

    if (!file_exists($patterns_directory)) {
        return;
    }

    $pattern_files = array_diff(scandir($patterns_directory), array('..', '.'));

    foreach ($pattern_files as $file) {
        $file_path = $patterns_directory . $file;

        if (!is_file($file_path) || pathinfo($file_path, PATHINFO_EXTENSION) !== 'php') {
            continue;
        }

        $pattern_data = get_file_data($file_path, array(
            'title'       => 'Title',
            'slug'        => 'Slug',
            'description' => 'Description',
            'categories'  => 'Categories',
            'keywords'    => 'Keywords',
            'viewport'    => 'ViewportWidth',
        ));

        if (empty($pattern_data['title'])) {
            continue;
        }
        ob_start();
        include $file_path;
        $pattern_content = ob_get_clean();

        if (empty($pattern_content)) {
            continue;
        }
        $categories = array('cuppa');
        if (!empty($pattern_data['categories'])) {
            $additional_cats = array_map('trim', explode(',', $pattern_data['categories']));
            $categories = array_merge($categories, $additional_cats);
        }
        register_block_pattern(
            'cuppa/' . (!empty($pattern_data['slug']) ? sanitize_title($pattern_data['slug']) : sanitize_title($pattern_data['title'])),
            array(
                'title'       => $pattern_data['title'],
                'description' => !empty($pattern_data['description']) ? $pattern_data['description'] : '',
                'content'     => $pattern_content,
                'categories'  => $categories,
                'keywords'    => !empty($pattern_data['keywords']) ? explode(',', $pattern_data['keywords']) : array(),
                'viewport'    => !empty($pattern_data['viewport']) ? (int) $pattern_data['viewport'] : null,
            )
        );
    }
}
add_action('init', 'cuppa_register_block_patterns', 9);
if ( ! function_exists( 'cuppa_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since Cuppa 1.0
	 *
	 * @return void
	 */
	function cuppa_block_styles() {
		register_block_style(
            'core/list',
            array(
                'name'         => 'cuppa-checkmark-list', 
                'label'        => __( 'Checkmark', 'cuppa' ),
                'inline_style' => '
                ul.is-style-cuppa-checkmark-list {
                    list-style-type: "\2713";
                }
                ul.is-style-cuppa-checkmark-list li {
                    padding-inline-start: 1ch;
                }',
            )
        );
	}
endif;
add_action( 'init', 'cuppa_block_styles' );

function cuppa_enqueue_block_styles() {
	wp_enqueue_style(
		'cuppa-block-styles',
		get_template_directory_uri() . '/assets/css/block-styles.css',
		array(),
		wp_get_theme()->get( 'Version' )
	);
}
add_action( 'enqueue_block_assets', 'cuppa_enqueue_block_styles' );


function cuppa_enqueue_theme_assets() {
    wp_enqueue_style(
        'cuppa-main-css',
        get_template_directory_uri() . '/assets/css/cuppa.css',
        array(),
        '1.0.0'
    );
    wp_enqueue_script(
        'cuppa-scrolltotop-js',
        get_template_directory_uri() . '/assets/js/scrolltotop.js',
        array(),
        wp_get_theme()->get('Version'),
        true 
    );
  
   }
add_action('wp_enqueue_scripts', 'cuppa_enqueue_theme_assets');

function cuppa_enqueue_swiper_assets() {
    wp_enqueue_style(
        'cuppa-swiper-css',
        get_template_directory_uri() . '/assets/css/swiper-bundle.min.css',
        array(),
        '11.0.0'
    );
    wp_enqueue_style(
        'cuppa-custom-swiper-css',
        get_template_directory_uri() . '/assets/css/cuppa-swiper.css',
        array(),
        '1.0.0'
    );
    wp_enqueue_script(
        'cuppa-swiper-js',
        get_template_directory_uri() . '/assets/js/swiper-bundle.min.js',
        array(),
        '11.0.0',
        true 
    );
    wp_enqueue_script(
        'cuppa-custom-swiper',
        get_template_directory_uri() . '/assets/js/cuppa-swiper.js',
        array('cuppa-swiper-js'), 
        '1.0.0',
        true 
    );
   }
add_action('wp_enqueue_scripts', 'cuppa_enqueue_swiper_assets');