<?php
/**
 * Vendor Asset Loader
 * Load CSS and JS files with local fallback
 */

class VendorLoader {
    private static $loaded = [];
    
    /**
     * Load CSS file
     * @param string $localPath
     * @param string $cdnUrl
     */
    public static function css($localPath, $cdnUrl = null) {
        $fullPath = __DIR__ . '/../assets/vendor/' . $localPath;
        
        if (file_exists($fullPath)) {
            echo '<link rel="stylesheet" href="assets/vendor/' . $localPath . '">' . "\n";
        } elseif ($cdnUrl) {
            echo '<link rel="stylesheet" href="' . $cdnUrl . '">' . "\n";
        }
    }
    
    /**
     * Load JS file
     * @param string $localPath
     * @param string $cdnUrl
     * @param bool $defer
     */
    public static function js($localPath, $cdnUrl = null, $defer = false) {
        $fullPath = __DIR__ . '/../assets/vendor/' . $localPath;
        $deferAttr = $defer ? ' defer' : '';
        
        if (file_exists($fullPath)) {
            echo '<script src="assets/vendor/' . $localPath . '"' . $deferAttr . '></script>' . "\n";
        } elseif ($cdnUrl) {
            echo '<script src="' . $cdnUrl . '"' . $deferAttr . '></script>' . "\n";
        }
    }
    
    /**
     * Load Bootstrap
     */
    public static function bootstrap() {
        self::css('bootstrap/css/bootstrap.min.css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css');
        self::js('bootstrap/js/bootstrap.bundle.min.js', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js');
    }
    
    /**
     * Load jQuery
     */
    public static function jquery() {
        self::js('jquery/jquery.min.js', 'https://code.jquery.com/jquery-3.7.0.min.js');
    }
    
    /**
     * Load DataTables
     */
    public static function datatables() {
        self::css('datatables/css/dataTables.bootstrap5.min.css', 'https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css');
        self::js('datatables/js/jquery.dataTables.min.js', 'https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js');
        self::js('datatables/js/dataTables.bootstrap5.min.js', 'https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js');
    }
    
    /**
     * Load Chart.js
     */
    public static function chartjs() {
        self::js('chartjs/chart.min.js', 'https://cdn.jsdelivr.net/npm/chart.js@4.3.0/dist/chart.umd.min.js');
    }
    
    /**
     * Load Summernote
     */
    public static function summernote() {
        self::css('summernote/css/summernote-bs4.min.css', 'https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-bs4.min.css');
        self::js('summernote/js/summernote-bs4.min.js', 'https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-bs4.min.js');
    }
    
    /**
     * Load Font Awesome
     */
    public static function fontawesome() {
        self::css('fontawesome/css/all.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');
    }
    
    /**
     * Load all essential vendors
     */
    public static function essentials() {
        self::bootstrap();
        self::jquery();
        self::fontawesome();
    }
    
    /**
     * Load all vendors
     */
    public static function all() {
        self::essentials();
        self::datatables();
        self::chartjs();
        self::summernote();
    }
    
    /**
     * Conditional loading based on page
     */
    public static function loadForPage($page) {
        self::essentials();
        
        if (strpos($page, 'list') !== false || strpos($page, 'index') !== false) {
            self::datatables();
        }
        
        if (strpos($page, 'report') !== false || strpos($page, 'dashboard') !== false) {
            self::chartjs();
        }
        
        if (strpos($page, 'add') !== false || strpos($page, 'edit') !== false) {
            self::summernote();
        }
    }
}