<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

define('IBVTE_ACCESS', true);
require_once 'config.php';

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

    // 1. Fix Categories (Departments) in course_categories
    echo "<h3>Fixing Categories...</h3>";
    $cats = [
        1 => ['Aviation Education', 'aviation'],
        2 => ['Automobile Education', 'automobile'],
        3 => ['Computer Education', 'computer-education'],
        4 => ['Ayurveda Education', 'ayurveda'],
        5 => ['Beautician Education', 'beautician']
    ];
    
    // We don't truncate, just insert ignore or update
    foreach ($cats as $id => $data) {
        $stmt = $pdo->prepare("SELECT id FROM course_categories WHERE id = ?");
        $stmt->execute([$id]);
        if ($stmt->fetch()) {
            $pdo->prepare("UPDATE course_categories SET name = ?, slug = ? WHERE id = ?")->execute([$data[0], $data[1], $id]);
        } else {
            $pdo->prepare("INSERT INTO course_categories (id, name, slug) VALUES (?, ?, ?)")->execute([$id, $data[0], $data[1]]);
        }
    }
    
    // 2. Fix Courses
    echo "<h3>Fixing Courses...</h3>";
    
    // Determine which schema we are on
    $hasCategoryId = false;
    try {
        $pdo->query("SELECT category_id FROM courses LIMIT 1");
        $hasCategoryId = true;
    } catch(Exception $e) {}

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

    // Update short_name based on course_code
    $mappings = [
        '%COMP%' => ['Computer Education', 3],
        '%AY%'   => ['Ayurveda Education', 4],
        '%AVI%'  => ['Aviation Education', 1],
        '%AUTO%' => ['Automobile Education', 2],
        '%BEA%'  => ['Beautician Education', 5]
    ];
    
    foreach ($mappings as $like => $info) {
        $name = $info[0];
        $cat_id = $info[1];
        
        // Update short_name
        $stmt = $pdo->prepare("UPDATE courses SET short_name = ? WHERE course_code LIKE ?");
        $stmt->execute([$name, $like]);
        
        // Update category_id if it exists
        if ($hasCategoryId) {
            $stmt = $pdo->prepare("UPDATE courses SET category_id = ? WHERE course_code LIKE ?");
            $stmt->execute([$cat_id, $like]);
        }
    }

    // Fix category column if it exists (append " Course" if needed)
    if ($hasCategory) {
        // e.g. If course_type is 'Diploma', category becomes 'Diploma Course'
        $pdo->exec("UPDATE courses SET category = 'Diploma Course' WHERE course_name LIKE '%Diploma%'");
        $pdo->exec("UPDATE courses SET category = 'Certificate Course' WHERE course_name LIKE '%Certificate%'");
        $pdo->exec("UPDATE courses SET category = 'Short Term Course' WHERE course_name LIKE '%Short Term%'");
        $pdo->exec("UPDATE courses SET category = 'Vocational Course' WHERE course_name LIKE '%Vocational%'");
    }

    echo "<h2 style='color:green'>Database Fixed Successfully!</h2>";
    echo "<p>You can now go back to <a href='all-courses.php'>All Courses</a>.</p>";

} catch (Exception $e) {
    echo "<h2 style='color:red'>Error: " . $e->getMessage() . "</h2>";
}
