<?php
/**
 * Fallback logo functionality
 *
 * @package Sophia
 * @since 1.0.0
 */
// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Conditionally show fallback logo only if:
 * - No custom logo is set
 * - No logo is output
 *
 * @param string $logo The existing logo HTML
 * @return string Modified logo HTML
 */
function sophia_theme_fallback_logo($logo)
{
    $custom_logo_id = get_theme_mod('custom_logo');
    
    if (empty($logo) && empty($custom_logo_id)) {
        $home_url  = esc_url(home_url('/'));
        $site_name = esc_attr(get_bloginfo('name'));
        
        $fallback_logo_url = apply_filters(
            'sophia_theme_fallback_logo_url',
            get_stylesheet_directory_uri() . '/assets/images/fallback-logo.svg'
        );
        
        $logo_img = sprintf(
            '<img class="fallback-logo" src="%s" alt="%s" style="max-width:205px;" width="205" height="auto" loading="lazy">',
            esc_url($fallback_logo_url),
            $site_name
        );
        
        $logo = sprintf(
            '<a href="%s" class="custom-logo-link" rel="home">%s</a>',
            $home_url,
            $logo_img
        );
    }
    
    return $logo;
}
add_filter('get_custom_logo', 'sophia_theme_fallback_logo');

/**
 * Check if site identity should be displayed in header
 * Hides title if it contains default values
 *
 * @since 1.0.0
 * @return bool True if site identity should be shown, false otherwise
 */
if (!function_exists('sophia_show_site_identity')) {
    function sophia_show_site_identity() {
        $site_title = trim(get_bloginfo('name'));
        
        // List of default titles that should hide the site identity
        $default_titles = array(
            'Just another WordPress site',
            'My WordPress Website',
            'My Great Blog',
	        'My Blog',
            'My blog',
            'WordPress Site',
            'My WordPress',
            'My New Website',
            'My Great Blog',
            'Untitled',
            'New WordPress Site',
            'Welcome to WordPress Playground',
            'Sophia',
            'Sophia Lite',
            ''
        
        );
        
        // Show site identity only if title has been customized
        return !in_array($site_title, $default_titles);
    }
}

/**
 * Hide site title and tagline blocks when using default values
 * This works with the block-based site-title and site-tagline blocks
 *
 * @since 1.0.0
 * @param string $block_content The block content
 * @param array $block The block data
 * @return string Modified block content
 */
function sophia_hide_default_site_identity($block_content, $block) {
    // Check if this is a site-title or site-tagline block
    if (isset($block['blockName']) && 
        ($block['blockName'] === 'core/site-title' || $block['blockName'] === 'core/site-tagline')) {
        
        // Use our existing function to check if we should show site identity
        if (!sophia_show_site_identity()) {
            return ''; // Return empty string to hide the block
        }
    }
    
    return $block_content;
}
add_filter('render_block', 'sophia_hide_default_site_identity', 10, 2);