<?php
require_once '../auth_helper.php';
require_login();

if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['id']) && isset($_GET['election_id'])) {
    $userId = $_GET['id'];
    $electionId = $_GET['election_id'];
    $pdo = db();

    try {
        // Remove the assignment for this election
        $stmt = $pdo->prepare("DELETE FROM election_assignments WHERE user_id = ? AND election_id = ? AND role_in_election = 'Voter'");
        $stmt->execute([$userId, $electionId]);

        // Optional: Log the action
        $currentUser = get_user();
        audit_log('voter_removed', 'users', $userId, null, null, "Removed voter ID $userId from election $electionId");

        header("Location: ../voter_management.php?success=voter_deleted");
        exit;
    } catch (PDOException $e) {
        die("Error deleting voter: " . $e->getMessage());
    }
} else {
    header("Location: ../voter_management.php");
    exit;
}
