<?php
/**
 * Add More Members Script
 * Adds children and youth members to the database
 * Run this to add more diverse age groups
 */

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

echo "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Adding More Members</title><style>body{font-family:monospace;padding:2rem;background:#f5f5f5;}pre{background:#fff;padding:1rem;border-radius:4px;}</style></head><body><h1>Adding More Members</h1><pre>";

echo "Starting to add more members (children and youth)...\n\n";

// Get existing households for family assignments
$households = [];
$stmt = $pdo->query("SELECT id FROM households ORDER BY id LIMIT 10");
foreach ($stmt->fetchAll() as $h) {
    $households[] = $h['id'];
}

// Create additional households for families with children
$newHouseholds = [];
$householdNames = [
    'Appiah Family',
    'Bonsu Family',
    'Danso Family',
    'Frimpong Family',
    'Gyamfi Family',
    'Kumi Family',
    'Lartey Family',
    'Nkrumah Family',
    'Ofori Family',
    'Quansah Family'
];

foreach ($householdNames as $name) {
    $stmt = $pdo->prepare("INSERT INTO households (name, address) VALUES (?, ?)");
    $stmt->execute([$name, rand(100, 999) . ' Street, Accra']);
    $newHouseholds[] = $pdo->lastInsertId();
    echo "Created household: $name\n";
}

$allHouseholds = array_merge($households, $newHouseholds);

// Get ministry IDs
$ministries = [];
$stmt = $pdo->query("SELECT id, name FROM ministries");
foreach ($stmt->fetchAll() as $m) {
    $ministries[$m['name']] = $m['id'];
}

// Children (ages 5-11) - Children's Service
$children = [
    // Boys
    ['title' => null, 'first_name' => 'Kofi', 'middle_name' => 'Junior', 'last_name' => 'Asante', 'gender' => 'Male', 'date_of_birth' => '2018-05-15', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2020-01-10', 'baptism_date' => null, 'household_id' => $allHouseholds[0]],
    ['title' => null, 'first_name' => 'Kwame', 'middle_name' => 'Kweku', 'last_name' => 'Owusu', 'gender' => 'Male', 'date_of_birth' => '2019-08-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2021-03-15', 'baptism_date' => null, 'household_id' => $allHouseholds[1]],
    ['title' => null, 'first_name' => 'Emmanuel', 'middle_name' => 'Yaw', 'last_name' => 'Mensah', 'gender' => 'Male', 'date_of_birth' => '2017-11-08', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2019-09-20', 'baptism_date' => null, 'household_id' => $allHouseholds[2]],
    ['title' => null, 'first_name' => 'Daniel', 'middle_name' => 'Nana', 'last_name' => 'Amoah', 'gender' => 'Male', 'date_of_birth' => '2016-04-12', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2018-06-10', 'baptism_date' => null, 'household_id' => $allHouseholds[3]],
    ['title' => null, 'first_name' => 'Michael', 'middle_name' => 'Kojo', 'last_name' => 'Boateng', 'gender' => 'Male', 'date_of_birth' => '2015-09-30', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2017-05-15', 'baptism_date' => null, 'household_id' => $allHouseholds[4]],
    ['title' => null, 'first_name' => 'David', 'middle_name' => 'Akwasi', 'last_name' => 'Appiah', 'gender' => 'Male', 'date_of_birth' => '2018-12-05', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2020-08-22', 'baptism_date' => null, 'household_id' => $allHouseholds[5]],
    ['title' => null, 'first_name' => 'Joseph', 'middle_name' => 'Fiifi', 'last_name' => 'Bonsu', 'gender' => 'Male', 'date_of_birth' => '2017-06-18', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2019-02-14', 'baptism_date' => null, 'household_id' => $allHouseholds[6]],
    ['title' => null, 'first_name' => 'Samuel', 'middle_name' => 'Kwaku', 'last_name' => 'Danso', 'gender' => 'Male', 'date_of_birth' => '2016-02-14', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2018-10-05', 'baptism_date' => null, 'household_id' => $allHouseholds[7]],
    
    // Girls
    ['title' => null, 'first_name' => 'Grace', 'middle_name' => 'Ama', 'last_name' => 'Asante', 'gender' => 'Female', 'date_of_birth' => '2018-05-20', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2020-01-10', 'baptism_date' => null, 'household_id' => $allHouseholds[0]],
    ['title' => null, 'first_name' => 'Mary', 'middle_name' => 'Akosua', 'last_name' => 'Owusu', 'gender' => 'Female', 'date_of_birth' => '2019-08-14', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2021-03-15', 'baptism_date' => null, 'household_id' => $allHouseholds[1]],
    ['title' => null, 'first_name' => 'Esther', 'middle_name' => 'Efua', 'last_name' => 'Mensah', 'gender' => 'Female', 'date_of_birth' => '2017-01-25', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2019-09-20', 'baptism_date' => null, 'household_id' => $allHouseholds[2]],
    ['title' => null, 'first_name' => 'Patience', 'middle_name' => 'Abena', 'last_name' => 'Amoah', 'gender' => 'Female', 'date_of_birth' => '2016-10-03', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2018-06-10', 'baptism_date' => null, 'household_id' => $allHouseholds[3]],
    ['title' => null, 'first_name' => 'Joyce', 'middle_name' => 'Adwoa', 'last_name' => 'Boateng', 'gender' => 'Female', 'date_of_birth' => '2015-03-17', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2017-05-15', 'baptism_date' => null, 'household_id' => $allHouseholds[4]],
    ['title' => null, 'first_name' => 'Ruth', 'middle_name' => 'Maame', 'last_name' => 'Appiah', 'gender' => 'Female', 'date_of_birth' => '2018-07-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2020-08-22', 'baptism_date' => null, 'household_id' => $allHouseholds[5]],
    ['title' => null, 'first_name' => 'Priscilla', 'middle_name' => 'Akua', 'last_name' => 'Bonsu', 'gender' => 'Female', 'date_of_birth' => '2017-02-09', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2019-02-14', 'baptism_date' => null, 'household_id' => $allHouseholds[6]],
    ['title' => null, 'first_name' => 'Deborah', 'middle_name' => 'Ama', 'last_name' => 'Danso', 'gender' => 'Female', 'date_of_birth' => '2016-04-30', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => null, 'occupation' => null, 'status' => 'child', 'join_date' => '2018-10-05', 'baptism_date' => null, 'household_id' => $allHouseholds[7]],
];

// Junior Youth (ages 13-17)
$juniorYouth = [
    // Boys
    ['title' => null, 'first_name' => 'Kofi', 'middle_name' => 'Junior', 'last_name' => 'Frimpong', 'gender' => 'Male', 'date_of_birth' => '2010-05-15', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2018-01-10', 'baptism_date' => '2015-06-20', 'household_id' => $allHouseholds[8]],
    ['title' => null, 'first_name' => 'Kwame', 'middle_name' => 'Kweku', 'last_name' => 'Gyamfi', 'gender' => 'Male', 'date_of_birth' => '2009-08-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2017-03-15', 'baptism_date' => '2014-08-15', 'household_id' => $allHouseholds[9]],
    ['title' => null, 'first_name' => 'Emmanuel', 'middle_name' => 'Yaw', 'last_name' => 'Kumi', 'gender' => 'Male', 'date_of_birth' => '2008-11-08', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2016-09-20', 'baptism_date' => '2013-12-10', 'household_id' => $allHouseholds[10]],
    ['title' => null, 'first_name' => 'Daniel', 'middle_name' => 'Nana', 'last_name' => 'Lartey', 'gender' => 'Male', 'date_of_birth' => '2007-04-12', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2015-06-10', 'baptism_date' => '2012-07-18', 'household_id' => $allHouseholds[11]],
    ['title' => null, 'first_name' => 'Michael', 'middle_name' => 'Kojo', 'last_name' => 'Nkrumah', 'gender' => 'Male', 'date_of_birth' => '2006-09-30', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2014-05-15', 'baptism_date' => '2011-11-22', 'household_id' => $allHouseholds[12]],
    ['title' => null, 'first_name' => 'David', 'middle_name' => 'Akwasi', 'last_name' => 'Ofori', 'gender' => 'Male', 'date_of_birth' => '2010-12-05', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2018-08-22', 'baptism_date' => '2016-03-14', 'household_id' => $allHouseholds[13]],
    
    // Girls
    ['title' => null, 'first_name' => 'Grace', 'middle_name' => 'Ama', 'last_name' => 'Frimpong', 'gender' => 'Female', 'date_of_birth' => '2010-05-20', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2018-01-10', 'baptism_date' => '2015-10-05', 'household_id' => $allHouseholds[8]],
    ['title' => null, 'first_name' => 'Mary', 'middle_name' => 'Akosua', 'last_name' => 'Gyamfi', 'gender' => 'Female', 'date_of_birth' => '2009-08-14', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2017-03-15', 'baptism_date' => '2014-01-20', 'household_id' => $allHouseholds[9]],
    ['title' => null, 'first_name' => 'Esther', 'middle_name' => 'Efua', 'last_name' => 'Kumi', 'gender' => 'Female', 'date_of_birth' => '2008-01-25', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2016-09-20', 'baptism_date' => '2013-05-15', 'household_id' => $allHouseholds[10]],
    ['title' => null, 'first_name' => 'Patience', 'middle_name' => 'Abena', 'last_name' => 'Lartey', 'gender' => 'Female', 'date_of_birth' => '2007-10-03', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2015-06-10', 'baptism_date' => '2012-07-22', 'household_id' => $allHouseholds[11]],
    ['title' => null, 'first_name' => 'Joyce', 'middle_name' => 'Adwoa', 'last_name' => 'Nkrumah', 'gender' => 'Female', 'date_of_birth' => '2006-03-17', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2014-05-15', 'baptism_date' => '2011-12-08', 'household_id' => $allHouseholds[12]],
    ['title' => null, 'first_name' => 'Ruth', 'middle_name' => 'Maame', 'last_name' => 'Ofori', 'gender' => 'Female', 'date_of_birth' => '2010-07-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Student', 'status' => 'youth', 'join_date' => '2018-08-22', 'baptism_date' => '2016-11-12', 'household_id' => $allHouseholds[13]],
];

// YPG (ages 18-29) - More youth
$ypgMembers = [
    // Males
    ['title' => 'Mr', 'first_name' => 'Kofi', 'middle_name' => 'Junior', 'last_name' => 'Quansah', 'gender' => 'Male', 'date_of_birth' => '2005-05-15', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2018-01-10', 'baptism_date' => '2012-06-20', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Kwame', 'middle_name' => 'Kweku', 'last_name' => 'Appiah', 'gender' => 'Male', 'date_of_birth' => '2004-08-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2017-03-15', 'baptism_date' => '2011-08-15', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Emmanuel', 'middle_name' => 'Yaw', 'last_name' => 'Bonsu', 'gender' => 'Male', 'date_of_birth' => '2003-11-08', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2016-09-20', 'baptism_date' => '2010-12-10', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Daniel', 'middle_name' => 'Nana', 'last_name' => 'Danso', 'gender' => 'Male', 'date_of_birth' => '2002-04-12', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2015-06-10', 'baptism_date' => '2009-07-18', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Michael', 'middle_name' => 'Kojo', 'last_name' => 'Frimpong', 'gender' => 'Male', 'date_of_birth' => '2001-09-30', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2014-05-15', 'baptism_date' => '2008-11-22', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'David', 'middle_name' => 'Akwasi', 'last_name' => 'Gyamfi', 'gender' => 'Male', 'date_of_birth' => '2000-12-05', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2013-08-22', 'baptism_date' => '2007-03-14', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Joseph', 'middle_name' => 'Fiifi', 'last_name' => 'Kumi', 'gender' => 'Male', 'date_of_birth' => '2005-06-18', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2018-02-14', 'baptism_date' => '2013-09-30', 'household_id' => null],
    ['title' => 'Mr', 'first_name' => 'Samuel', 'middle_name' => 'Kwaku', 'last_name' => 'Lartey', 'gender' => 'Male', 'date_of_birth' => '2004-02-14', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2017-10-05', 'baptism_date' => '2012-04-25', 'household_id' => null],
    
    // Females
    ['title' => 'Miss', 'first_name' => 'Grace', 'middle_name' => 'Ama', 'last_name' => 'Quansah', 'gender' => 'Female', 'date_of_birth' => '2005-05-20', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2018-01-10', 'baptism_date' => '2012-10-05', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Mary', 'middle_name' => 'Akosua', 'last_name' => 'Appiah', 'gender' => 'Female', 'date_of_birth' => '2004-08-14', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2017-03-15', 'baptism_date' => '2011-01-20', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Esther', 'middle_name' => 'Efua', 'last_name' => 'Bonsu', 'gender' => 'Female', 'date_of_birth' => '2003-01-25', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2016-09-20', 'baptism_date' => '2010-05-15', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Patience', 'middle_name' => 'Abena', 'last_name' => 'Danso', 'gender' => 'Female', 'date_of_birth' => '2002-10-03', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2015-06-10', 'baptism_date' => '2009-07-22', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Joyce', 'middle_name' => 'Adwoa', 'last_name' => 'Frimpong', 'gender' => 'Female', 'date_of_birth' => '2001-03-17', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2014-05-15', 'baptism_date' => '2008-12-08', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Ruth', 'middle_name' => 'Maame', 'last_name' => 'Gyamfi', 'gender' => 'Female', 'date_of_birth' => '2000-07-22', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'Graduate', 'status' => 'youth', 'join_date' => '2013-08-22', 'baptism_date' => '2007-11-12', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Priscilla', 'middle_name' => 'Akua', 'last_name' => 'Kumi', 'gender' => 'Female', 'date_of_birth' => '2005-02-09', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2018-02-14', 'baptism_date' => '2013-08-30', 'household_id' => null],
    ['title' => 'Miss', 'first_name' => 'Deborah', 'middle_name' => 'Ama', 'last_name' => 'Lartey', 'gender' => 'Female', 'date_of_birth' => '2004-04-30', 'phone' => '0243658609', 'email' => 'appnationals@gmail.com', 'marital_status' => 'Single', 'occupation' => 'University Student', 'status' => 'youth', 'join_date' => '2017-10-05', 'baptism_date' => '2012-09-25', 'household_id' => null],
];

$allNewMembers = array_merge($children, $juniorYouth, $ypgMembers);

// Insert members
$stmt = $pdo->prepare("INSERT INTO members (title, first_name, middle_name, last_name, gender, date_of_birth, phone, email, marital_status, occupation, status, join_date, baptism_date, address, household_id, emergency_contact, emergency_phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$memberIds = [];
$insertedCount = 0;

foreach ($allNewMembers as $member) {
    $emergencyContact = 'Parent';
    $emergencyPhone = '0243658609';
    $address = $member['household_id'] ? 'Family Address' : rand(100, 999) . ' Street, Accra';
    
    try {
        $stmt->execute([
            $member['title'],
            $member['first_name'],
            $member['middle_name'],
            $member['last_name'],
            $member['gender'],
            $member['date_of_birth'],
            $member['phone'],
            $member['email'],
            $member['marital_status'],
            $member['occupation'],
            $member['status'],
            $member['join_date'],
            $member['baptism_date'],
            $address,
            $member['household_id'],
            $emergencyContact,
            $emergencyPhone
        ]);
        
        $memberId = $pdo->lastInsertId();
        $memberIds[] = $memberId;
        $insertedCount++;
        
        $name = trim(($member['title'] ?? '') . ' ' . $member['first_name'] . ' ' . ($member['middle_name'] ?? '') . ' ' . $member['last_name']);
        echo "Added: $name (ID: $memberId)\n";
    } catch (PDOException $e) {
        echo "Error adding " . ($member['first_name'] . ' ' . $member['last_name']) . ": " . $e->getMessage() . "\n";
    }
}

// Assign some members to ministries
if (!empty($memberIds) && isset($ministries['Youth Ministry'])) {
    $youthMinistryId = $ministries['Youth Ministry'];
    $stmt = $pdo->prepare("INSERT INTO member_ministries (member_id, ministry_id) VALUES (?, ?)");
    
    // Assign all youth and YPG members to Youth Ministry
    $youthStartIndex = count($children);
    $youthEndIndex = count($children) + count($juniorYouth) + count($ypgMembers);
    
    for ($i = $youthStartIndex; $i < $youthEndIndex && $i < count($memberIds); $i++) {
        try {
            $stmt->execute([$memberIds[$i], $youthMinistryId]);
        } catch (PDOException $e) {
            // Ignore duplicates
        }
    }
    echo "\nAssigned youth members to Youth Ministry\n";
}

if (!empty($memberIds) && isset($ministries["Children's Ministry"])) {
    $childrenMinistryId = $ministries["Children's Ministry"];
    $stmt = $pdo->prepare("INSERT INTO member_ministries (member_id, ministry_id) VALUES (?, ?)");
    
    // Assign children to Children's Ministry
    for ($i = 0; $i < count($children) && $i < count($memberIds); $i++) {
        try {
            $stmt->execute([$memberIds[$i], $childrenMinistryId]);
        } catch (PDOException $e) {
            // Ignore duplicates
        }
    }
    echo "Assigned children to Children's Ministry\n";
}

echo "\n\n=== Summary ===\n";
echo "Children added: " . count($children) . "\n";
echo "Junior Youth added: " . count($juniorYouth) . "\n";
echo "YPG members added: " . count($ypgMembers) . "\n";
echo "Total new members: $insertedCount\n";
echo "New households created: " . count($newHouseholds) . "\n";

echo "</pre>";
echo "<p><a href='/nazareth/members'>View Members</a> | <a href='/nazareth/members/analytics'>View Analytics</a></p>";
echo "</body></html>";
