<?php
/**
 * One-time: fill every 2026 Sunday preaching plan with hypothetical occasions and participants
 * (preacher, liturgist, readers) to match the Epiphany-style layout.
 * Run: php install/seed-preaching-plan-participants-2026.php
 * DELETE this file after use.
 */

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

$year = 2026;

// Hypothetical participants (rotate through these for variety)
$preachers = [
    'Rev. Abban Teiko',
    'Rev. Samuel Addo',
    'Rev. Grace Mensah',
    'Eld. Joseph Asante',
];
$liturgistsFirst = [
    'Eld. Dinah Komey',
    'Cat. Mary Osei',
    'Eld. Paul Ampofo',
    'Cat. Comfort Boateng',
];
$liturgistsForenoon = [
    'Cat. Ebenezer O. Torto',
    'Eld. Daniel Tetteh',
    'Cat. Abigail Laryea',
    'Eld. Philip Owusu',
];
$gaReaders = [
    'Eld. Grace Laryea',
    'Mr. Kofi Laryea',
    'Ms. Abena Nkansah',
    'Eld. Yaw Addo',
];
$twiReaders = [
    'Ms. Astra Asare',
    'Mr. Stephen Dakroh',
    'Ms. Akua Mensah',
    'Mr. Kwame Sarpong',
];
$englishReaders = [
    'Ms Martha Ayim Adu',
    'Mr. Godfred Aryettey',
    'Ms. Esther Botwe',
    'Mr. David Nkrumah',
];
// Occasions – liturgical-style, cycle through
$occasions = [
    'Epiphany Of The Lord',
    'Second Sunday After Epiphany',
    'Third Sunday After Epiphany',
    'Fourth Sunday After Epiphany',
    'Fifth Sunday After Epiphany',
    'Sunday Worship',
    'Transfiguration Sunday',
    'First Sunday In Lent',
    'Second Sunday In Lent',
    'Third Sunday In Lent',
    'Fourth Sunday In Lent',
    'Palm Sunday',
    'Easter Sunday',
    'Second Sunday Of Easter',
    'Third Sunday Of Easter',
    'Fourth Sunday Of Easter',
    'Fifth Sunday Of Easter',
    'Ascension Sunday',
    'Pentecost Sunday',
    'Trinity Sunday',
    'Second Sunday After Pentecost',
    'Third Sunday After Pentecost',
    'Fourth Sunday After Pentecost',
    'Fifth Sunday After Pentecost',
    'Sixth Sunday After Pentecost',
    'Seventh Sunday After Pentecost',
    'Eighth Sunday After Pentecost',
    'Ninth Sunday After Pentecost',
    'Tenth Sunday After Pentecost',
    'Harvest Thanksgiving',
    'All Saints Sunday',
    'Reign Of Christ',
    'First Sunday Of Advent',
    'Second Sunday Of Advent',
    'Third Sunday Of Advent',
    'Fourth Sunday Of Advent',
    'Christmas Sunday',
];
$remarksOptions = ['Covenant Service', 'Holy Communion', 'Thanksgiving', 'Prayer and Praise', ''];

$stmt = $pdo->prepare("
    SELECT id, service_date, service_type
    FROM preaching_plan
    WHERE service_date >= ? AND service_date <= ?
    ORDER BY service_date, service_type
");
$stmt->execute([$year . '-01-01', $year . '-12-31']);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$updateStmt = $pdo->prepare("
    UPDATE preaching_plan
    SET occasion = ?, preacher = ?, liturgist = ?, ga_bible_reader = ?, twi_bible_reader = ?, english_bible_reader = ?, remarks = ?
    WHERE id = ?
");

$sundayIndex = -1;
$updated = 0;

foreach ($rows as $row) {
    $id = (int) $row['id'];
    $date = $row['service_date'];
    $serviceType = $row['service_type'];

    // New Sunday when we see first-service
    if ($serviceType === 'first-service') {
        $sundayIndex++;
    }

    $occIdx = $sundayIndex % count($occasions);
    $occasion = $occasions[$occIdx];
    $preacher = $preachers[$sundayIndex % count($preachers)];
    $remarks = $remarksOptions[$sundayIndex % count($remarksOptions)];

    if ($serviceType === 'first-service') {
        $liturgist = $liturgistsFirst[$sundayIndex % count($liturgistsFirst)];
        $gaReader = null; // First Service has no Ga reader
        $twiReader = $twiReaders[$sundayIndex % count($twiReaders)];
        $engReader = $englishReaders[$sundayIndex % count($englishReaders)];
    } else {
        $liturgist = $liturgistsForenoon[$sundayIndex % count($liturgistsForenoon)];
        $gaReader = $gaReaders[$sundayIndex % count($gaReaders)];
        $twiReader = $twiReaders[($sundayIndex + 1) % count($twiReaders)]; // different from first service
        $engReader = $englishReaders[($sundayIndex + 1) % count($englishReaders)];
    }

    $updateStmt->execute([
        $occasion,
        $preacher,
        $liturgist,
        $gaReader ?? '',
        $twiReader,
        $engReader,
        $remarks,
        $id,
    ]);
    $updated++;
}

$total = $pdo->query("SELECT COUNT(*) FROM preaching_plan WHERE service_date >= '$year-01-01' AND service_date <= '$year-12-31'")->fetchColumn();

if (php_sapi_name() === 'cli') {
    echo "Updated $updated preaching plan entries for $year with hypothetical participants.\n";
    echo "Total 2026 entries: $total\n";
} else {
    header('Content-Type: text/html; charset=utf-8');
    echo "<!DOCTYPE html><html><head><title>Preaching Plan Participants</title></head><body>";
    echo "<h1>Preaching plan 2026 – participants</h1>";
    echo "<p><strong>$updated</strong> entries updated with hypothetical occasions and participants (preacher, liturgist, readers, remarks).</p>";
    echo "<p>Total 2026 entries: <strong>$total</strong></p>";
    echo "<p><a href='" . BASE_PATH . "/events/preaching-plan'>Preaching Plan</a> | <a href='" . BASE_PATH . "/events?month=2026-01'>Events January 2026</a></p>";
    echo "<p style='color:#64748b;'>Delete install/seed-preaching-plan-participants-2026.php after use.</p>";
    echo "</body></html>";
}
