<?php
define('IBVTE_ACCESS', true);
require 'config.php';
require 'includes/db.php';

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $isLive = false;
    try { $pdo->query("SELECT category_id FROM courses LIMIT 1"); $isLive = true; } catch(Exception $e) {}

    $coursesData = [
        // MUSIC EDUCATION
        ['IBVTE-MUS-001', 'Diploma in Vocal Music', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Viva', '80000', 'Music Education'],
        ['IBVTE-MUS-002', 'Diploma in Instrumental Music', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical Examination', '80000', 'Music Education'],
        ['IBVTE-MUS-003', 'Diploma in Hindustani Classical Music', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Performance + Viva', '80000', 'Music Education'],
        ['IBVTE-MUS-004', 'Diploma in Carnatic Music', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Theory', '80000', 'Music Education'],
        ['IBVTE-MUS-005', 'Diploma in Music Production & Sound Engineering', 'Diploma', '1 Year', '10+2', 'Regular', 'Studio Project + Practical', '80000', 'Music Education'],
        ['IBVTE-MUS-006', 'Diploma in Audio Recording Technology', 'Diploma', '1 Year', '10+2', 'Regular', 'Practical + Project', '80000', 'Music Education'],
        ['IBVTE-MUS-007', 'Diploma in Music Composition & Arrangement', 'Diploma', '1 Year', '10+2', 'Regular', 'Composition Project', '80000', 'Music Education'],
        ['IBVTE-MUS-008', 'Diploma in Keyboard & Piano Performance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Performance Assessment', '80000', 'Music Education'],
        ['IBVTE-MUS-009', 'Diploma in Guitar Performance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Viva', '80000', 'Music Education'],
        ['IBVTE-MUS-010', 'Diploma in Music Teaching & Education', 'Diploma', '1 Year', '10+2', 'Regular', 'Teaching Practice', '80000', 'Music Education'],
        ['IBVTE-MUS-011', 'Certificate in Vocal Music', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Test', '40000', 'Music Education'],
        ['IBVTE-MUS-012', 'Certificate in Guitar Playing', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Performance Assessment', '40000', 'Music Education'],
        ['IBVTE-MUS-013', 'Certificate in Keyboard Playing', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Examination', '40000', 'Music Education'],
        ['IBVTE-MUS-014', 'Certificate in Tabla Performance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical + Viva', '40000', 'Music Education'],
        ['IBVTE-MUS-015', 'Certificate in Harmonium Playing', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Test', '40000', 'Music Education'],
        ['IBVTE-MUS-016', 'Certificate in Music Production Basics', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Studio Practical', '40000', 'Music Education'],
        ['IBVTE-MUS-017', 'Certificate in Sound Recording Techniques', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Project Assessment', '40000', 'Music Education'],
        ['IBVTE-MUS-018', 'Certificate in Music Theory', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Theory + Viva', '40000', 'Music Education'],
        ['IBVTE-MUS-019', 'Short Term Course in Basic Singing Skills', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Performance Test', '15000', 'Music Education'],
        ['IBVTE-MUS-020', 'Short Term Course in Guitar Fundamentals', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical Assessment', '15000', 'Music Education'],
        ['IBVTE-MUS-021', 'Short Term Course in Keyboard Fundamentals', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical Test', '15000', 'Music Education'],
        ['IBVTE-MUS-022', 'Short Term Course in Music Mixing & Editing', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Studio Practical', '15000', 'Music Education'],
        ['IBVTE-MUS-023', 'Short Term Course in Home Studio Setup', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Project Assessment', '15000', 'Music Education'],
        ['IBVTE-MUS-024', 'Short Term Course in Bhajan & Devotional Singing', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Performance Test', '15000', 'Music Education'],
        ['IBVTE-MUS-025', 'Short Term Course in Music Performance Skills', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical + Viva', '15000', 'Music Education'],

        // DANCE EDUCATION
        ['IBVTE-DAN-001', 'Diploma in Dance Performance Arts', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Stage Performance', '80000', 'Dance Education'],
        ['IBVTE-DAN-002', 'Diploma in Choreography & Dance Direction', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Project + Performance', '80000', 'Dance Education'],
        ['IBVTE-DAN-003', 'Diploma in Bollywood Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical Assessment', '80000', 'Dance Education'],
        ['IBVTE-DAN-004', 'Diploma in Contemporary Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Viva', '80000', 'Dance Education'],
        ['IBVTE-DAN-005', 'Diploma in Hip-Hop & Street Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Performance Test', '80000', 'Dance Education'],
        ['IBVTE-DAN-006', 'Diploma in Kathak Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Theory', '80000', 'Dance Education'],
        ['IBVTE-DAN-007', 'Diploma in Bharatanatyam Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Viva', '80000', 'Dance Education'],
        ['IBVTE-DAN-008', 'Diploma in Folk & Cultural Dance', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Stage Performance', '80000', 'Dance Education'],
        ['IBVTE-DAN-009', 'Diploma in Dance Teaching & Education', 'Diploma', '1 Year', '10+2', 'Regular', 'Teaching Practice', '80000', 'Dance Education'],
        ['IBVTE-DAN-010', 'Diploma in Dance Production & Stage Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Project + Viva', '80000', 'Dance Education'],
        ['IBVTE-DAN-011', 'Certificate in Dance Performance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Assessment', '40000', 'Dance Education'],
        ['IBVTE-DAN-012', 'Certificate in Bollywood Dance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Performance Test', '40000', 'Dance Education'],
        ['IBVTE-DAN-013', 'Certificate in Contemporary Dance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Assessment', '40000', 'Dance Education'],
        ['IBVTE-DAN-014', 'Certificate in Hip-Hop Dance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Performance Test', '40000', 'Dance Education'],
        ['IBVTE-DAN-015', 'Certificate in Kathak Dance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical + Theory', '40000', 'Dance Education'],
        ['IBVTE-DAN-016', 'Certificate in Bharatanatyam Dance', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Assessment', '40000', 'Dance Education'],
        ['IBVTE-DAN-017', 'Certificate in Choreography Skills', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Project Work', '40000', 'Dance Education'],
        ['IBVTE-DAN-018', 'Certificate in Dance Fitness & Zumba', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Test', '40000', 'Dance Education'],
        ['IBVTE-DAN-019', 'Short Term Course in Basic Dance Skills', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical Test', '15000', 'Dance Education'],
        ['IBVTE-DAN-020', 'Short Term Course in Bollywood Dance Techniques', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Performance Assessment', '15000', 'Dance Education'],
        ['IBVTE-DAN-021', 'Short Term Course in Hip-Hop Fundamentals', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical Test', '15000', 'Dance Education'],
        ['IBVTE-DAN-022', 'Short Term Course in Wedding Choreography', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Project Performance', '15000', 'Dance Education'],
        ['IBVTE-DAN-023', 'Short Term Course in Dance Fitness Instructor', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Assessment', '15000', 'Dance Education'],
        ['IBVTE-DAN-024', 'Short Term Course in Folk Dance Training', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Performance Test', '15000', 'Dance Education'],
        ['IBVTE-DAN-025', 'Short Term Course in Stage Performance Skills', 'Short Term', '3 Months', 'No Formal Qualification', 'Regular', 'Practical + Viva', '15000', 'Dance Education'],

        // TECHNICAL EDUCATION
        ['IBVTE-TTE-001', 'Diploma in Industrial Technician Engineering', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Theory + Practical', '80000', 'Technical Education'],
        ['IBVTE-TTE-002', 'Diploma in Mechanical Maintenance Technology', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Workshop + Practical', '80000', 'Technical Education'],
        ['IBVTE-TTE-003', 'Diploma in Electrical Maintenance Technology', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Theory + Practical', '80000', 'Technical Education'],
        ['IBVTE-TTE-004', 'Diploma in Industrial Automation Technology', 'Diploma', '1 Year', '10+2', 'Regular', 'PLC Project + Practical', '80000', 'Technical Education'],
        ['IBVTE-TTE-005', 'Diploma in Mechatronics Technology', 'Diploma', '1 Year', '10+2', 'Regular', 'Practical + Project', '80000', 'Technical Education'],
        ['IBVTE-TTE-006', 'Diploma in CNC Machine Operations', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Workshop Assessment', '80000', 'Technical Education'],
        ['IBVTE-TTE-007', 'Diploma in Welding Technology', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical Test', '80000', 'Technical Education'],
        ['IBVTE-TTE-008', 'Diploma in Refrigeration & Air Conditioning Technology', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Practical + Viva', '80000', 'Technical Education'],
        ['IBVTE-TTE-009', 'Diploma in Solar Energy Technology', 'Diploma', '1 Year', '10th Pass', 'Regular', 'Project + Field Work', '80000', 'Technical Education'],
        ['IBVTE-TTE-010', 'Diploma in Industrial Safety & Maintenance', 'Diploma', '1 Year', '10+2', 'Regular', 'Theory + Practical', '80000', 'Technical Education'],
        ['IBVTE-TTE-011', 'Certificate in Electrician Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Assessment', '40000', 'Technical Education'],
        ['IBVTE-TTE-012', 'Certificate in Fitter Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Workshop Test', '40000', 'Technical Education'],
        ['IBVTE-TTE-013', 'Certificate in Welding Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Examination', '40000', 'Technical Education'],
        ['IBVTE-TTE-014', 'Certificate in CNC Programming Basics', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Practical + Viva', '40000', 'Technical Education'],
        ['IBVTE-TTE-015', 'Certificate in Refrigeration & AC Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Assessment', '40000', 'Technical Education'],
        ['IBVTE-TTE-016', 'Certificate in Solar Panel Installation Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Field Practical', '40000', 'Technical Education'],
        ['IBVTE-TTE-017', 'Certificate in Industrial Automation Basics', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Project Work', '40000', 'Technical Education'],
        ['IBVTE-TTE-018', 'Certificate in Maintenance Technician', 'Certificate', '6 Months', '8th Pass', 'Regular', 'Practical Test', '40000', 'Technical Education'],
        ['IBVTE-TTE-019', 'Short Term Course in Basic Electrical Wiring', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Test', '15000', 'Technical Education'],
        ['IBVTE-TTE-020', 'Short Term Course in Welding Fundamentals', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Assessment', '15000', 'Technical Education'],
        ['IBVTE-TTE-021', 'Short Term Course in Solar Installation Skills', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Field Test', '15000', 'Technical Education'],
        ['IBVTE-TTE-022', 'Short Term Course in CNC Machine Operation', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Workshop Assessment', '15000', 'Technical Education'],
        ['IBVTE-TTE-023', 'Short Term Course in Industrial Safety Practices', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Theory + Practical', '15000', 'Technical Education'],
        ['IBVTE-TTE-024', 'Short Term Course in Pump & Motor Maintenance', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Test', '15000', 'Technical Education'],
        ['IBVTE-TTE-025', 'Short Term Course in Workshop Assistant Training', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical + Viva', '15000', 'Technical Education'],

        // OTHER VOCATIONAL COURSE
        ['IBVTE-OVC-001', 'Diploma in Entrepreneurship Development', 'Diploma', '1 Year', '10+2', 'Regular', 'Project + Viva', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-002', 'Diploma in Office Administration', 'Diploma', '1 Year', '10+2', 'Regular', 'Theory + Practical', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-003', 'Diploma in Event Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Project + Internship', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-004', 'Diploma in Retail Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Practical + Viva', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-005', 'Diploma in Digital Entrepreneurship', 'Diploma', '1 Year', '10+2', 'Regular', 'Project Work', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-006', 'Diploma in Public Relations & Communication', 'Diploma', '1 Year', '10+2', 'Regular', 'Theory + Presentation', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-007', 'Diploma in Community Development & Social Work', 'Diploma', '1 Year', '10+2', 'Regular', 'Field Work + Viva', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-008', 'Diploma in NGO Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Project Assessment', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-009', 'Diploma in E-Commerce Business Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Practical + Project', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-010', 'Diploma in Customer Relationship Management', 'Diploma', '1 Year', '10+2', 'Regular', 'Theory + Practical', '80000', 'Other Vocational Course'],
        ['IBVTE-OVC-011', 'Certificate in Entrepreneurship Skills', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Project Work', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-012', 'Certificate in Event Planning & Coordination', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Practical Assessment', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-013', 'Certificate in Retail Sales Management', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Practical + Viva', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-014', 'Certificate in E-Commerce Operations', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Practical Assessment', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-015', 'Certificate in Customer Service Excellence', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Theory + Practical', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-016', 'Certificate in Communication Skills & Personality Development', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Presentation + Viva', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-017', 'Certificate in Social Work & Community Service', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Field Assessment', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-018', 'Certificate in NGO Administration', 'Certificate', '6 Months', '10th Pass', 'Regular', 'Project Work', '40000', 'Other Vocational Course'],
        ['IBVTE-OVC-019', 'Short Term Course in Personality Development', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Assessment', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-020', 'Short Term Course in Spoken English & Communication', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Oral Test', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-021', 'Short Term Course in Customer Service Skills', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Test', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-022', 'Short Term Course in Retail Sales Associate', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Assessment', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-023', 'Short Term Course in Entrepreneurship Basics', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Project Work', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-024', 'Short Term Course in Office Assistant Skills', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Practical Test', '15000', 'Other Vocational Course'],
        ['IBVTE-OVC-025', 'Short Term Course in Interview & Career Readiness Skills', 'Short Term', '3 Months', '8th Pass', 'Regular', 'Viva + Practical', '15000', 'Other Vocational Course'],
    ];

    $count = 0;
    foreach ($coursesData as $course) {
        $code = $course[0];
        $name = $course[1];
        $type = $course[2];
        $duration = $course[3];
        $eligibility = $course[4];
        $mode = 'offline';
        $fee = floatval($course[7]);
        $catName = $course[8];

        $durParts = explode(' ', $duration);
        $dur_v = $durParts[0];
        $dur_t = count($durParts) > 1 ? strtolower($durParts[1]) : 'months';
        if ($dur_t == 'year') $dur_t = 'years';
        
        $dur_m = 0;
        if ($dur_t == 'years') {
            $dur_m = intval($dur_v) * 12;
        } else if ($dur_t == 'months') {
            $dur_m = intval($dur_v);
        }

        $typeSlugLive = '';
        $typeSlugLocal = '';
        if (stripos($type, 'diploma') !== false) {
            $typeSlugLive = '2'; 
            $typeSlugLocal = 'diploma';
        } else if (stripos($type, 'short term') !== false) {
            $typeSlugLive = '1';
            $typeSlugLocal = 'certification'; 
        } else if (stripos($type, 'certificate') !== false) {
            $typeSlugLive = '1';
            $typeSlugLocal = 'certification';
        }

        if ($isLive) {
            // Find category
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $catName), '-'));
            $stmt = $pdo->prepare("SELECT id FROM course_categories WHERE slug = ? OR name = ? ORDER BY display_order ASC LIMIT 1");
            $stmt->execute([$slug, $catName]);
            $catId = $stmt->fetchColumn();

            if (!$catId) {
                $stmt = $pdo->prepare("INSERT INTO course_categories (name, slug, display_order, is_active) VALUES (?, ?, 0, 1)");
                $stmt->execute([$catName, $slug]);
                $catId = $pdo->lastInsertId();
            }

            // Find or create sub_category
            $subStmt = $pdo->prepare("SELECT id FROM course_sub_categories WHERE category_id = ? AND name LIKE ?");
            $subStmt->execute([$catId, $type . '%']);
            $subCatId = $subStmt->fetchColumn();
            if (!$subCatId) {
                $subSlug = $slug . '-' . strtolower(str_replace(' ', '-', $type));
                $pdo->prepare("INSERT INTO course_sub_categories (category_id, name, slug, is_active) VALUES (?, ?, ?, 1)")->execute([$catId, $type . ' Course', $subSlug]);
                $subCatId = $pdo->lastInsertId();
            }

            // Insert course
            $stmt = $pdo->prepare("INSERT INTO courses (category_id, sub_category_id, course_type, course_name, course_code, short_name, duration_text, duration_months, duration_value, eligibility, mode, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([$catId, $subCatId, $typeSlugLive, $name, $code, $catName, $duration, $dur_m, $dur_v, $eligibility, $mode, 1]);
            $courseId = $pdo->lastInsertId();
            
            // Insert fee
            $stmt = $pdo->prepare("INSERT INTO course_fees (course_id, user_type, admission_fee, exam_fee, total_fee, valid_from) VALUES (?, ?, 0, 0, ?, '2026-01-01')");
            $stmt->execute([$courseId, 'college', $fee]);
            
        } else {
            // Local Schema
            $stmt = $pdo->prepare("INSERT INTO courses (course_code, course_name, short_name, category, course_type, duration, duration_months, duration_type, duration_value, eligibility, mode, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([$code, $name, $catName, $type . ' Course', $typeSlugLocal, $duration, $dur_m, $dur_t, $dur_v, $eligibility, $mode, 1]);
            $courseId = $pdo->lastInsertId();
            
            // Insert fee
            $stmt = $pdo->prepare("INSERT INTO course_fees (course_id, user_type, admission_fee, exam_fee, total_fee, valid_from) VALUES (?, ?, 0, 0, ?, '2026-01-01')");
            $stmt->execute([$courseId, 'college', $fee]);

            // Add category if not exists
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $catName), '-'));
            $stmt = $pdo->prepare("SELECT id FROM course_categories WHERE name = ?");
            $stmt->execute([$catName]);
            if (!$stmt->fetchColumn()) {
                $stmt = $pdo->prepare("INSERT INTO course_categories (name, slug, display_order, is_active) VALUES (?, ?, 0, 1)");
                $stmt->execute([$catName, $slug]);
            }
        }
        $count++;
    }

    echo "Successfully imported $count courses!\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
