<?php

require_once __DIR__ . '/db.php';

function sparrow_get_daily_verse(PDO $pdo): array
{
    // Fetch the raw daily verse list from settings
    $stmt = $pdo->prepare("
        SELECT value 
        FROM backyard_settings 
        WHERE category = 'daily_verse_list'
        LIMIT 1
    ");
    $stmt->execute();
    $raw = $stmt->fetchColumn() ?: '';

    // Split by lines
    $lines = array_filter(array_map('trim', explode("\n", $raw)));

    if (empty($lines)) {
        return ['reference' => '', 'text' => ''];
    }

    // Pick one at random (or implement sequential logic if you want)
    $random_line = $lines[array_rand($lines)];

    // Split into reference and text
    if (strpos($random_line, '|') !== false) {
        list($reference, $text) = array_map('trim', explode('|', $random_line, 2));
    } else {
        $reference = '';
        $text = $random_line;
    }

    return ['reference' => $reference, 'text' => $text];
}

function sparrow_get_daily_prayer(PDO $pdo): string
{
    $stmt = $pdo->prepare("
        SELECT value 
        FROM backyard_settings 
        WHERE category = 'daily_prayer'
        LIMIT 1
    ");
    $stmt->execute();
    return $stmt->fetchColumn() ?: '';
}