<?php
/**
 * Font Loader
 * Handles conditional loading of custom fonts
 *
 * Load Switzer font if available
 *
 * This function checks if the Switzer Variable font has been manually uploaded
 * by the user to /wp-content/uploads/fonts/ and conditionally loads it.
 * 
 * THEME REVIEW NOTES:
 * -------------------
 * 1. LICENSING COMPLIANCE:
 *    - Switzer font is NOT bundled with this theme due to licensing restrictions
 *    - Font must be downloaded separately from Fontshare.com (free, open license)
 *    - Users receive clear instructions in README.txt for manual installation
 * 
 * 2. WHY THIS APPROACH:
 *    - WordPress.org themes cannot bundle fonts with unclear licenses
 *    - This allows users to legally obtain and use the designer's preferred font
 *    - Graceful fallback to system fonts if Switzer is not installed
 * 
 * 3. HOW IT WORKS:
 *    - Checks if Switzer-Variable.woff2 exists in uploads/fonts/
 *    - If found: Generates @font-face rule to load the font
 *    - If not found: theme.json font stack falls back to system fonts
 *    - No errors or warnings if font is missing (fail-safe design)
 * 
 * 4. USER EXPERIENCE:
 *    - Theme works perfectly with OR without the custom font
 *    - System font fallbacks maintain design integrity
 *    - README.txt provides step-by-step installation instructions
 * 
 * 5. INTEGRATION WITH THEME.JSON:
 *    - theme.json declares "Switzer" font family with fallback stack
 *    - This function only adds @font-face if user uploaded the font file
 *    - No modifications to WordPress core or forced external dependencies
 *
 * @package Sophia
 * @since 1.0.0
 */

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

/**
 * Load Switzer font if available
 *
 * Checks for Switzer font file in uploads directory and loads it
 * if found. Falls back gracefully to theme's font stack if not available.
 *
 * @since 1.0.0
 * @return void
 */
function sophia_add_switzer_font() {
    $upload_dir = wp_upload_dir();
    $font_path = $upload_dir['basedir'] . '/fonts/Switzer-Variable.woff2';
    
    if (file_exists($font_path)) {
        $font_url = esc_url($upload_dir['baseurl'] . '/fonts/Switzer-Variable.woff2');
        
        // Create dedicated handle for font
        wp_register_style('sophia-switzer-font', false);
        wp_enqueue_style('sophia-switzer-font');
        
        // Load the font
        wp_add_inline_style('sophia-switzer-font', "
            @font-face {
                font-family: 'Switzer';
                src: url('{$font_url}') format('woff2');
                font-weight: 100 900;
                font-display: swap;
                font-style: normal;
            }
        ");
    }
}
add_action('wp_enqueue_scripts', 'sophia_add_switzer_font');
add_action('enqueue_block_editor_assets', 'sophia_add_switzer_font');