<?php
require_once 'config/database.php';

$db = getDB();

// Read and execute the trigger SQL file
$sql = file_get_contents('database/triggers/trg_after_exam_attempt_submit.sql');

// Split by DELIMITER changes and execute each part
$parts = preg_split('/DELIMITER\s+\/\//', $sql);

foreach ($parts as $part) {
    $part = trim($part);
    if (empty($part)) continue;

    // Replace // with ; for execution
    $part = str_replace('//', ';', $part);

    try {
        $db->exec($part);
        echo "Executed part successfully\n";
    } catch (Exception $e) {
        echo "Error executing part: " . $e->getMessage() . "\n";
    }
}

echo "Trigger installation completed\n";
?>