<?php
/**
 * Vendor Assets Loader
 * Load CSS/JS with local fallback if CDN fails
 */

// ============================================
// CSS Assets
// ============================================

function enqueue_css($name, $local_path, $cdn_url) {
    $local_file = __DIR__ . '/../assets/vendor/' . $local_path;
    if (file_exists($local_file)) {
        echo '<link rel="stylesheet" href="assets/vendor/' . $local_path . '">' . "\n";
    } else {
        echo '<link rel="stylesheet" href="' . $cdn_url . '">' . "\n";
    }
}

function enqueue_js($name, $local_path, $cdn_url, $defer = false) {
    $defer_attr = $defer ? ' defer' : '';
    $local_file = __DIR__ . '/../assets/vendor/' . $local_path;
    if (file_exists($local_file)) {
        echo '<script src="assets/vendor/' . $local_path . '"' . $defer_attr . '></script>' . "\n";
    } else {
        echo '<script src="' . $cdn_url . '"' . $defer_attr . '></script>' . "\n";
    }
}

// ============================================
// Load CSS Files
// ============================================

function load_vendor_css() {
    ?>
    <!-- Bootstrap 5 CSS -->
    <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    
    <!-- Font Awesome 6 -->
    <link href="assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- DataTables CSS -->
    <link href="assets/vendor/datatables/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css">
    
    <!-- Summernote CSS -->
    <link href="assets/vendor/summernote/css/summernote-bs4.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-bs4.min.css">
    <?php
}

// ============================================
// Load JS Files
// ============================================

function load_vendor_js() {
    ?>
    <!-- jQuery -->
    <script src="assets/vendor/jquery/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script>window.jQuery || document.write('<script src="assets/vendor/jquery/jquery.min.js"><\/script>');</script>
    
    <!-- Bootstrap 5 JS -->
    <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- DataTables JS -->
    <script src="assets/vendor/datatables/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script src="assets/vendor/datatables/js/dataTables.bootstrap5.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js"></script>
    
    <!-- Chart.js -->
    <script src="assets/vendor/chartjs/chart.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.0/dist/chart.umd.min.js"></script>
    
    <!-- Summernote JS -->
    <script src="assets/vendor/summernote/js/summernote-bs4.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-bs4.min.js"></script>
    <?php
}

// ============================================
// Combined Loader
// ============================================

function load_vendor_assets() {
    load_vendor_css();
    load_vendor_js();
}

// ============================================
// Individual Component Loaders
// ============================================

function load_bootstrap() {
    echo '<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
    echo '<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>';
}

function load_jquery() {
    echo '<script src="assets/vendor/jquery/jquery.min.js"></script>';
}

function load_datatables() {
    echo '<link href="assets/vendor/datatables/css/dataTables.bootstrap5.min.css" rel="stylesheet">';
    echo '<script src="assets/vendor/datatables/js/jquery.dataTables.min.js"></script>';
    echo '<script src="assets/vendor/datatables/js/dataTables.bootstrap5.min.js"></script>';
}

function load_chartjs() {
    echo '<script src="assets/vendor/chartjs/chart.min.js"></script>';
}

function load_summernote() {
    echo '<link href="assets/vendor/summernote/css/summernote-bs4.min.css" rel="stylesheet">';
    echo '<script src="assets/vendor/summernote/js/summernote-bs4.min.js"></script>';
}

function load_fontawesome() {
    echo '<link href="assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">';
}

// ============================================
// Conditional Loading
// ============================================

function load_chart_if_needed() {
    if (strpos($_SERVER['REQUEST_URI'], 'reports') !== false || 
        strpos($_SERVER['REQUEST_URI'], 'dashboard') !== false) {
        load_chartjs();
    }
}

function load_datatables_if_needed() {
    if (strpos($_SERVER['REQUEST_URI'], 'list') !== false || 
        strpos($_SERVER['REQUEST_URI'], 'index') !== false) {
        load_datatables();
    }
}

function load_summernote_if_needed() {
    if (strpos($_SERVER['REQUEST_URI'], 'add') !== false || 
        strpos($_SERVER['REQUEST_URI'], 'edit') !== false) {
        load_summernote();
    }
}
?>