<?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);
    
    // Check if category_id exists
    $hasCatId = false;
    try { $pdo->query("SELECT category_id FROM courses LIMIT 1"); $hasCatId = true; } catch(Exception $e) {}

    $courses = [
        ['IBVTE-BEA-001', 'Diploma in Beauty Therapy & Cosmetology', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 85000],
        ['IBVTE-BEA-002', 'Diploma in Professional Makeup Artistry', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 95000],
        ['IBVTE-BEA-003', 'Diploma in Hair Designing & Styling', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 75000],
        ['IBVTE-BEA-004', 'Diploma in Salon Management', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 65000],
        ['IBVTE-BEA-005', 'Diploma in Advanced Cosmetology', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 120000],
        ['IBVTE-BEA-006', 'Diploma in Spa Therapy & Wellness', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 80000],
        ['IBVTE-BEA-007', 'Diploma in Bridal Makeup & Beauty Care', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 90000],
        ['IBVTE-BEA-008', 'Diploma in Nail Technology & Art', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 70000],
        ['IBVTE-BEA-009', 'Diploma in Skin Care & Aesthetics', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 95000],
        ['IBVTE-BEA-010', 'Diploma in Hair & Beauty Management', 'Diploma Course', 'diploma', '1 Year', '10th', 'offline', 80000],
        ['IBVTE-BEA-011', 'Certificate in Beautician Course', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 25000],
        ['IBVTE-BEA-012', 'Certificate in Beauty Culture', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 28000],
        ['IBVTE-BEA-013', 'Certificate in Makeup Artistry', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 35000],
        ['IBVTE-BEA-014', 'Certificate in Hair Cutting & Styling', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 30000],
        ['IBVTE-BEA-015', 'Certificate in Bridal Makeup', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 40000],
        ['IBVTE-BEA-016', 'Certificate in Nail Art & Extensions', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 32000],
        ['IBVTE-BEA-017', 'Certificate in Facial & Skin Care', 'Certificate Course', 'certification', '6 Months', '8th Pass', 'offline', 30000],
    ];

    foreach ($courses as $c) {
        // check if exists
        $stmt = $pdo->prepare("SELECT id FROM courses WHERE course_code = ?");
        $stmt->execute([$c[0]]);
        $existing = $stmt->fetch();
        
        $dur_m = strpos($c[4], 'Year') !== false ? 12 : 6;
        $dur_t = strpos($c[4], 'Year') !== false ? 'years' : 'months';
        $dur_v = strpos($c[4], 'Year') !== false ? 1 : 6;
        
        if (!$existing) {
            if ($hasCatId) {
                // Live schema
                $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([5, 1, $c[2], $c[1], $c[0], 'Beautician Education', $c[4], $dur_m, $dur_v, $c[5], $c[6], 1]);
            } 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([$c[0], $c[1], 'Beautician Education', $c[2], $c[3], $c[4], $dur_m, $dur_t, $dur_v, $c[5], $c[6], 1]);
            }
            $courseId = $pdo->lastInsertId();
            
            // Insert fee
            $pdo->prepare("INSERT INTO course_fees (course_id, user_type, admission_fee, exam_fee, total_fee, valid_from) VALUES (?, 'college', 0, 0, ?, '2026-01-01')")->execute([$courseId, $c[7]]);
        }
    }
    
    echo "Added successfully!";
} catch(Exception $e) {
    echo "Error: " . $e->getMessage();
}
