/**
 * Course Filter JavaScript
 * Handles course filtering functionality
 */

(function($) {
    'use strict';
    
    $(document).ready(function() {
        const courseFilter = {
            init: function() {
                this.bindEvents();
                this.initFilters();
            },
            
            bindEvents: function() {
                $('#level-filter, #category-filter').on('change', this.filterCourses.bind(this));
                
                // Search functionality
                $('#course-search').on('input', this.debounce(this.searchCourses.bind(this), 300));
            },
            
            initFilters: function() {
                // Initialize filter state
                this.currentFilters = {
                    level: '',
                    category: '',
                    search: ''
                };
            },
            
            filterCourses: function() {
                const level = $('#level-filter').val();
                const category = $('#category-filter').val();
                
                this.currentFilters.level = level;
                this.currentFilters.category = category;
                
                this.applyFilters();
            },
            
            searchCourses: function(e) {
                const searchTerm = $(e.target).val().toLowerCase();
                this.currentFilters.search = searchTerm;
                this.applyFilters();
            },
            
            applyFilters: function() {
                const $courses = $('.course-card');
                let visibleCount = 0;
                
                $courses.each(function() {
                    const $course = $(this);
                    const courseLevel = $course.data('level');
                    const courseCategories = $course.data('categories').split(' ');
                    const courseTitle = $course.find('.course-title').text().toLowerCase();
                    const courseDescription = $course.find('.course-description').text().toLowerCase();
                    
                    let showCourse = true;
                    
                    // Level filter
                    if (this.currentFilters.level && courseLevel !== this.currentFilters.level) {
                        showCourse = false;
                    }
                    
                    // Category filter
                    if (this.currentFilters.category && !courseCategories.includes(this.currentFilters.category)) {
                        showCourse = false;
                    }
                    
                    // Search filter
                    if (this.currentFilters.search) {
                        const searchTerm = this.currentFilters.search;
                        if (!courseTitle.includes(searchTerm) && !courseDescription.includes(searchTerm)) {
                            showCourse = false;
                        }
                    }
                    
                    if (showCourse) {
                        $course.fadeIn(300);
                        visibleCount++;
                    } else {
                        $course.fadeOut(300);
                    }
                });
                
                this.updateResultsCount(visibleCount);
            },
            
            updateResultsCount: function(count) {
                let $countElement = $('.course-results-count');
                
                if ($countElement.length === 0) {
                    $countElement = $('<div class="course-results-count"></div>');
                    $('.courses-grid').before($countElement);
                }
                
                $countElement.text(`${count} course${count !== 1 ? 's' : ''} found`);
            },
            
            debounce: function(func, wait) {
                let timeout;
                return function executedFunction(...args) {
                    const later = () => {
                        clearTimeout(timeout);
                        func(...args);
                    };
                    clearTimeout(timeout);
                    timeout = setTimeout(later, wait);
                };
            }
        };
        
        // Initialize course filter
        courseFilter.init();
    });
    
})(jQuery);
