<?php
require_once __DIR__ . '/../../db/config.php';

try {
    $pdoconn = db();
    $pdoconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "CREATE TABLE IF NOT EXISTS meeting_attendance (
        id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        meeting_id INT(11) UNSIGNED NOT NULL,
        person_id INT(11) UNSIGNED NOT NULL,
        attendance_status ENUM('present', 'absent', 'substitute', 'none') NOT NULL DEFAULT 'none',
        guest_survey ENUM('1', '2', '3'),
        updated_by INT(11) UNSIGNED,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        UNIQUE KEY meeting_person (meeting_id, person_id),
        FOREIGN KEY (meeting_id) REFERENCES meetings(id) ON DELETE CASCADE,
        FOREIGN KEY (person_id) REFERENCES people(id) ON DELETE CASCADE,
        FOREIGN KEY (updated_by) REFERENCES people(id) ON DELETE SET NULL
    )";

    $pdoconn->exec($sql);
    echo "Table 'meeting_attendance' created successfully." . PHP_EOL;
} catch (PDOException $e) {
    echo "Error creating table: " . $e->getMessage() . PHP_EOL;
    exit(1);
}
