<?php
declare(strict_types=1);
require_once __DIR__ . '/../auth_helper.php';
require_login();
require_role(['Admin', 'Adviser', 'Officer']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $election_id = $_POST['election_id'] ?? '';
    $name = $_POST['name'] ?? '';
    $description = $_POST['description'] ?? '';

    if (!$election_id || !$name) {
        die("Missing fields");
    }

    try {
        $pdo = db();
        $id = uuid();
        $stmt = $pdo->prepare("INSERT INTO parties (id, election_id, name, description) VALUES (?, ?, ?, ?)");
        $stmt->execute([$id, $election_id, $name, $description]);

        audit_log('Added party', 'parties', $id);

        header("Location: ../candidate_management.php?success=1");
        exit;
    } catch (Exception $e) {
        die($e->getMessage());
    }
}
