<?php
// admin/cron-bet-of-the-day.php
// Run this script daily to send Bet of the Day alerts to all active users.

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/includes/EmailService.php';

// Ensure it's run from CLI or browser (for testing)
if (php_sapi_name() !== 'cli' && (!isset($_GET['key']) || $_GET['key'] !== 'cron_secret')) {
    die("Access denied.");
}

$emailService = new EmailService($pdo);

// Get all active users
$stmt = $pdo->query("SELECT id, full_name, email FROM users WHERE status = 'ACTIVE'");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

$count = 0;
foreach ($users as $user) {
    $result = $emailService->sendTemplateEmail('bet_of_the_day_alert', $user['email'], [
        'user_name'          => $user['full_name'],
        'bet_of_the_day_link' => SITE_URL . '/bet-of-the-day.php'
    ]);
    if ($result) $count++;
}

echo "Bet of the Day alerts sent to {$count} users.\n";
?>