<?php
/**
 * BW Default Title Block
 * 
 * Outputs the appropriate title for any page type including:
 * - Standard pages
 * - Archives (category, tag, date, author, etc)
 * - 404 pages
 * - Search results
 */

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

/**
 * Register the block
 */
function bw_register_default_title_block() {
    // Register block script
    wp_register_script(
        'bw-default-title-block',
        get_stylesheet_directory_uri() . '/blocks/bw-default-title/block.js',
        array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-server-side-render'),
        filemtime(get_stylesheet_directory() . '/blocks/bw-default-title/block.js')
    );
    
    // Register block editor style
    wp_register_style(
        'bw-default-title-block-editor',
        get_stylesheet_directory_uri() . '/blocks/bw-default-title/editor.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-default-title/editor.css')
    );
    
    // Register the block
    register_block_type('bw/default-title', array(
        'editor_script' => 'bw-default-title-block',
        'editor_style' => 'bw-default-title-block-editor',
        'render_callback' => 'bw_default_title_block_render',
        'attributes' => array(
            'align' => array(
                'type' => 'string',
                'default' => 'left'
            ),
            'tag' => array(
                'type' => 'string',
                'default' => 'h1'
            ),
            'className' => array(
                'type' => 'string',
                'default' => ''
            )
        )
    ));
}
add_action('init', 'bw_register_default_title_block');

/**
 * Render the block
 */
function bw_default_title_block_render($attributes) {
    // Extract attributes with defaults
    $align = isset($attributes['align']) ? $attributes['align'] : 'left';
    $tag = isset($attributes['tag']) ? $attributes['tag'] : 'h1';
    $class_name = isset($attributes['className']) ? $attributes['className'] : '';
    
    // Start output buffering
    ob_start();
    
    // Main class
    $classes = 'bw-default-title';
    
    // Add alignment class
    if ($align) {
        $classes .= ' has-text-align-' . $align;
    }
    
    // Add custom class if provided
    if ($class_name) {
        $classes .= ' ' . $class_name;
    }
    
    // Get the appropriate title based on current page
    $title = bw_get_dynamic_title();
    
    // Output the title with appropriate tag
    printf('<%1$s class="%2$s">%3$s</%1$s>', 
        esc_attr($tag),
        esc_attr($classes),
        wp_kses_post($title)
    );
    
    // Return the buffered output
    return ob_get_clean();
}

/**
 * Get the dynamic title based on the current page type
 */
function bw_get_dynamic_title() {
    // Default title (if all else fails)
    $title = get_bloginfo('name');
    
    // 404 page
    if (is_404()) {
        $title = __('Page Not Found', 'kadence-child');
    }
    // Search results
    elseif (is_search()) {
        /* translators: %s: search query */
        $title = sprintf(__('Search Results for: %s', 'kadence-child'), get_search_query());
    }
    // Front page
    elseif (is_front_page()) {
        // Use site title if specifically set to front page
        if (get_option('show_on_front') === 'page') {
            $front_page_id = get_option('page_on_front');
            $title = get_the_title($front_page_id);
        }
    }
    // Blog page
    elseif (is_home()) {
        $blog_page_id = get_option('page_for_posts');
        if ($blog_page_id) {
            $title = get_the_title($blog_page_id);
        } else {
            $title = __('Blog', 'kadence-child');
        }
    }
    // Category archive
    elseif (is_category()) {
        /* translators: %s: category name */
        $title = sprintf(__('Category: %s', 'kadence-child'), single_cat_title('', false));
    }
    // Tag archive
    elseif (is_tag()) {
        /* translators: %s: tag name */
        $title = sprintf(__('Tag: %s', 'kadence-child'), single_tag_title('', false));
    }
    // Author archive
    elseif (is_author()) {
        /* translators: %s: author name */
        $title = sprintf(__('Author: %s', 'kadence-child'), get_the_author());
    }
    // Day archive
    elseif (is_day()) {
        /* translators: %s: day date */
        $title = sprintf(__('Day: %s', 'kadence-child'), get_the_date());
    }
    // Month archive
    elseif (is_month()) {
        /* translators: %s: month date */
        $title = sprintf(__('Month: %s', 'kadence-child'), get_the_date('F Y'));
    }
    // Year archive
    elseif (is_year()) {
        /* translators: %s: year date */
        $title = sprintf(__('Year: %s', 'kadence-child'), get_the_date('Y'));
    }
    // Post type archive
    elseif (is_post_type_archive()) {
        /* translators: %s: post type name */
        $title = sprintf(__('Archives: %s', 'kadence-child'), post_type_archive_title('', false));
    }
    // Tax archive
    elseif (is_tax()) {
        $tax = get_taxonomy(get_queried_object()->taxonomy);
        /* translators: %1$s: taxonomy term name, %2$s: taxonomy name */
        $title = sprintf(__('%1$s: %2$s', 'kadence-child'), 
            $tax->labels->singular_name,
            single_term_title('', false)
        );
    }
    // Single post or page
    elseif (is_singular()) {
        $title = get_the_title();
    }
    
    // Allow filtering of the title (useful for custom page types)
    return apply_filters('bw_default_title_block_content', $title);
}

/**
 * Note: The custom block category is already registered in the Gallery Slider block,
 * so we don't need to register it again here to avoid function redeclaration.
 */