<?php
/**
 * Delete a member permanently (admin only).
 */

require_once __DIR__ . '/../../includes/member-delete.php';

if (!canDeleteMembers()) {
    header('Location: ' . BASE_PATH . '/members?toast_error=' . urlencode('Only administrators can delete members.'));
    exit;
}

$memberId = (int) ($id ?? 0);
if ($memberId < 1) {
    header('Location: ' . BASE_PATH . '/members/remove?toast_error=' . urlencode('That member link is invalid. Search again from Remove member.'));
    exit;
}

$result = nazarethDeleteMember($pdo, $memberId);

if (!$result['ok']) {
    $msg = ($result['error'] ?? '') === 'Member not found.'
        ? 'That member is no longer in the system (they may already have been deleted). Search again if you need to remove someone else.'
        : ('Could not delete member: ' . ($result['error'] ?? 'unknown error'));
    header('Location: ' . BASE_PATH . '/members/remove?toast_error=' . urlencode($msg));
    exit;
}

$toast = 'Member “' . $result['name'] . '” was permanently deleted.';
if (!empty($result['warnings'])) {
    $toast .= ' ' . implode(' ', $result['warnings']);
}

header('Location: ' . BASE_PATH . '/members/remove?toast=' . urlencode($toast));
exit;
