<?php
// Script to add missing foreign key constraints to assignment_submissions table

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

try {
    // Add foreign key constraints to assignment_submissions table
    $sql = "
        ALTER TABLE `assignment_submissions` 
        ADD CONSTRAINT `assignment_submissions_ibfk_2` FOREIGN KEY (`assignment_id`) REFERENCES `assignments` (`id`) ON DELETE CASCADE,
        ADD CONSTRAINT `assignment_submissions_ibfk_3` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE;
    ";
    
    $pdo->exec($sql);
    
    echo "Foreign key constraints added to assignment_submissions table successfully!\n";
    
} catch (PDOException $e) {
    // Check if the constraints already exist
    if (strpos($e->getMessage(), 'Duplicate foreign key constraint name') !== false) {
        echo "Foreign key constraints already exist in assignment_submissions table.\n";
    } else {
        echo "Error adding foreign key constraints: " . $e->getMessage() . "\n";
    }
}

?>