<?php
/**
 * One-time fix: Remove ministry assignments for members under 18 (children and junior youth).
 * Only YPG and above (18+) should belong to ministries.
 */

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

echo "Fixing ministry assignments by date of birth...\n";

// Members under 18: date_of_birth > (today - 18 years)
// Delete from member_ministries where member is under 18
$stmt = $pdo->prepare("
    DELETE mm FROM member_ministries mm
    INNER JOIN members m ON m.id = mm.member_id
    WHERE m.date_of_birth IS NOT NULL
    AND m.date_of_birth > DATE_SUB(CURDATE(), INTERVAL 18 YEAR)
");
$stmt->execute();
$removed = $stmt->rowCount();

echo "Removed $removed ministry assignment(s) for members under 18.\n";

// Show summary: how many members under 18 had ministries before (we just removed them)
$stmt = $pdo->query("
    SELECT COUNT(DISTINCT m.id) as under18
    FROM members m
    WHERE m.date_of_birth IS NOT NULL
    AND m.date_of_birth > DATE_SUB(CURDATE(), INTERVAL 18 YEAR)
");
$under18 = $stmt->fetchColumn();
echo "Members under 18 in database: $under18 (none should have ministry assignments now).\n";
echo "Done.\n";
