<?php
/**
 * Migration: Remove Exam References from Tables
 * Updates cart_items to only support 'course' item type and removes exam-related data
 */

require_once __DIR__ . '/../config/database.php';

$db = getDB();

try {
    $db->beginTransaction();

    // Remove exam rows from cart_items
    echo "Removing exam items from cart_items...\n";
    $db->exec("DELETE FROM cart_items WHERE item_type = 'exam'");

    // Alter cart_items to only support 'course'
    echo "Altering cart_items table...\n";
    $db->exec("ALTER TABLE cart_items MODIFY COLUMN item_type ENUM('course') NOT NULL DEFAULT 'course'");

    // Also update the payment_transactions table if it has 'exam' item_type
    echo "Updating payment_transactions table...\n";
    try {
        $db->exec("DELETE FROM payment_transactions WHERE item_type = 'exam'");
        $db->exec("ALTER TABLE payment_transactions MODIFY COLUMN item_type ENUM('course', 'cart') NOT NULL DEFAULT 'course'");
    } catch (PDOException $e) {
        echo "Note: payment_transactions may not exist or have exam type: " . $e->getMessage() . "\n";
    }

    $db->commit();
    echo "Exam references removed successfully!\n";

} catch (PDOException $e) {
    $db->rollBack();
    echo "Error: " . $e->getMessage() . "\n";
    exit(1);
}