<?php
/**
 * Enqueue scripts and styles
 * 
 * All Bootstrap files are loaded locally from the theme.
 * No CDN dependencies — this theme runs forever.
 */

if (!defined('ABSPATH')) exit;

function wpbs_enqueue_assets() {
    $theme_uri = WPBS_THEME_URI;
    $version = WPBS_THEME_VERSION;
    $bs_path = '/assets/bootstrap/';

    // --- CSS ---

    // Bootstrap 5.3.3 (local)
    $bs_css = $theme_uri . $bs_path . 'css/bootstrap.min.css';
    if (file_exists(WPBS_THEME_DIR . $bs_path . 'css/bootstrap.min.css')) {
        wp_enqueue_style('bootstrap', $bs_css, [], '5.3.3');
    } else {
        // Fallback to CDN only if local files haven't been downloaded yet
        wp_enqueue_style('bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css', [], '5.3.3');
    }

    // Bootstrap Icons (local)
    $icons_css = $theme_uri . $bs_path . 'css/bootstrap-icons.min.css';
    if (file_exists(WPBS_THEME_DIR . $bs_path . 'css/bootstrap-icons.min.css')) {
        wp_enqueue_style('bootstrap-icons', $icons_css, [], '1.11.3');
    } else {
        wp_enqueue_style('bootstrap-icons', 'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css', [], '1.11.3');
    }

    // Font Awesome 6 Free (local)
    $fa_css = $theme_uri . '/assets/fontawesome/css/all.min.css';
    if (file_exists(WPBS_THEME_DIR . '/assets/fontawesome/css/all.min.css')) {
        wp_enqueue_style('font-awesome', $fa_css, [], '6.5.1');
    } else {
        wp_enqueue_style('font-awesome', 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css', [], '6.5.1');
    }

    // Theme stylesheet (loads after Bootstrap so it can override)
    wp_enqueue_style('wpbs-theme', get_stylesheet_uri(), ['bootstrap'], $version);

    // --- JS ---

    // Bootstrap Bundle (includes Popper) — local
    $bs_js = $theme_uri . $bs_path . 'js/bootstrap.bundle.min.js';
    if (file_exists(WPBS_THEME_DIR . $bs_path . 'js/bootstrap.bundle.min.js')) {
        wp_enqueue_script('bootstrap', $bs_js, [], '5.3.3', true);
    } else {
        wp_enqueue_script('bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js', [], '5.3.3', true);
    }

    // Theme JS (minimal — offcanvas behavior etc.)
    wp_enqueue_script('wpbs-theme', $theme_uri . '/assets/js/theme.js', ['bootstrap'], $version, true);

    // Remove jQuery if not needed (speed optimization)
    if (!is_admin()) {
        // Only deregister if no other plugin needs it
        // wp_deregister_script('jquery');
    }

    // Comment reply script
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}
add_action('wp_enqueue_scripts', 'wpbs_enqueue_assets');

/**
 * Preload critical assets for speed
 */
function wpbs_preload_assets() {
    $bs_path = WPBS_THEME_URI . '/assets/bootstrap/';
    
    // Preload Bootstrap CSS
    if (file_exists(WPBS_THEME_DIR . '/assets/bootstrap/css/bootstrap.min.css')) {
        echo '<link rel="preload" href="' . esc_url($bs_path . 'css/bootstrap.min.css') . '" as="style">' . "\n";
    }
    
    // Preload icon fonts if local (css/fonts/ matches the @font-face path in bootstrap-icons.min.css)
    if (file_exists(WPBS_THEME_DIR . '/assets/bootstrap/css/fonts/bootstrap-icons.woff2')) {
        echo '<link rel="preload" href="' . esc_url($bs_path . 'css/fonts/bootstrap-icons.woff2') . '" as="font" type="font/woff2" crossorigin>' . "\n";
    }
}
add_action('wp_head', 'wpbs_preload_assets', 1);

/**
 * Add async/defer to non-critical scripts
 */
function wpbs_script_attributes($tag, $handle) {
    $defer_scripts = ['wpbs-theme'];
    
    if (in_array($handle, $defer_scripts)) {
        return str_replace(' src', ' defer src', $tag);
    }
    return $tag;
}
add_filter('script_loader_tag', 'wpbs_script_attributes', 10, 2);
