<?php
/**
 * One-time: fill preaching_plan with all Sundays in 2026 (first-service + forenoon per Sunday).
 * Run once: php install/seed-preaching-plan-2026.php
 * Or in browser: /nazareth/install/seed-preaching-plan-2026.php
 * DELETE this file after use.
 */

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

// Ensure table exists (do not change structure if already present)
try {
    $pdo->query("SELECT 1 FROM preaching_plan LIMIT 1");
} catch (PDOException $e) {
    $pdo->exec("
        CREATE TABLE preaching_plan (
            id INT AUTO_INCREMENT PRIMARY KEY,
            service_date DATE NOT NULL,
            service_type VARCHAR(20) DEFAULT 'first-service',
            occasion VARCHAR(200),
            preacher VARCHAR(200),
            liturgist VARCHAR(200),
            ga_bible_reader VARCHAR(200),
            twi_bible_reader VARCHAR(200),
            english_bible_reader VARCHAR(200),
            remarks VARCHAR(200),
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_service_date (service_date),
            INDEX idx_service_type (service_type)
        )
    ");
}

$checkStmt = $pdo->prepare("SELECT 1 FROM preaching_plan WHERE service_date = ? AND service_type = ?");

$year = 2026;
$start = new DateTime("$year-01-01");
$end   = new DateTime("$year-12-31");

$dayOfWeek = (int) $start->format('w');
if ($dayOfWeek !== 0) {
    $start->modify('+' . (7 - $dayOfWeek) . ' days');
}

$stmt = $pdo->prepare("
    INSERT INTO preaching_plan (service_date, service_type, occasion)
    VALUES (?, ?, ?)
");

$sundays = 0;
$inserted = 0;

while ($start <= $end) {
    $dateStr = $start->format('Y-m-d');
    $sundays++;

    foreach (['first-service', 'forenoon'] as $serviceType) {
        $checkStmt->execute([$dateStr, $serviceType]);
        if ($checkStmt->fetch()) {
            continue;
        }
        $stmt->execute([$dateStr, $serviceType, 'Sunday Worship']);
        $inserted++;
    }

    $start->modify('+7 days');
}

$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 "Preaching plan 2026: $sundays Sundays, $inserted row(s) inserted.\n";
    echo "Total entries for 2026: $total\n";
} else {
    header('Content-Type: text/html; charset=utf-8');
    echo "<!DOCTYPE html><html><head><title>Preaching Plan 2026</title></head><body>";
    echo "<h1>Preaching plan 2026</h1>";
    echo "<p><strong>$sundays</strong> Sundays. <strong>$inserted</strong> row(s) inserted.</p>";
    echo "<p>Total entries for 2026: <strong>$total</strong> (First Service + Forenoon per Sunday).</p>";
    echo "<p><a href='" . BASE_PATH . "/events/preaching-plan'>View Preaching Plan</a> | <a href='" . BASE_PATH . "/events'>Events</a></p>";
    echo "<p style='color:#64748b;'>Delete install/seed-preaching-plan-2026.php after use.</p>";
    echo "</body></html>";
}
