<?php
/**
 * Upgrade: add election assignment (group_type, group_id, group_name, manager_user_id, enabled)
 * Run once to add columns for admin-created elections assigned to Gen Grp or Service Grp.
 */

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

$alter = [
    "ALTER TABLE elections ADD COLUMN enabled TINYINT(1) DEFAULT 1 AFTER status",
    "ALTER TABLE elections ADD COLUMN group_type VARCHAR(20) NULL COMMENT 'gen_grp or service_grp' AFTER enabled",
    "ALTER TABLE elections ADD COLUMN group_id INT NULL COMMENT 'ministry id when group_type=service_grp' AFTER group_type",
    "ALTER TABLE elections ADD COLUMN group_name VARCHAR(100) NULL COMMENT 'fellowship name when group_type=gen_grp' AFTER group_id",
    "ALTER TABLE elections ADD COLUMN manager_user_id INT NULL AFTER group_name",
];

foreach ($alter as $sql) {
    try {
        $pdo->exec($sql);
        echo "OK: " . substr($sql, 0, 60) . "...\n";
    } catch (PDOException $e) {
        if (strpos($e->getMessage(), 'Duplicate column') !== false) {
            echo "Column already exists, skipping.\n";
        } else {
            echo "Error: " . $e->getMessage() . "\n";
        }
    }
}

echo "Elections assignment upgrade complete.\n";
