<?php
/**
 * Daily Notification Cron Job
 * Sends push notifications to all users at 9 AM IST
 * 
 * Schedule this script to run daily at 9:00 AM IST:
 * 0 9 * * * /usr/bin/php /path/to/birthday_reminder/cron/send_daily_notifications.php
 * 
 * For Windows Task Scheduler:
 * Program: C:\path\to\php.exe
 * Arguments: C:\path\to\birthday_reminder\cron\send_daily_notifications.php
 * Schedule: Daily at 9:00 AM
 */

// Set timezone to IST
date_default_timezone_set('Asia/Kolkata');

// Load configuration and autoloader
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/autoload.php';

// Log script start
$logMessage = "\n" . str_repeat('=', 80) . "\n";
$logMessage .= "Daily Notification Cron Job Started\n";
$logMessage .= "Date/Time: " . date('Y-m-d H:i:s') . " IST\n";
$logMessage .= str_repeat('=', 80) . "\n";
echo $logMessage;
logError($logMessage);

try {
    // Create notification service
    $notificationService = new NotificationService();
    
    // Check service status
    $status = $notificationService->getStatus();
    echo "Service Status:\n";
    echo "  - VAPID Configured: " . ($status['vapid_configured'] ? 'Yes' : 'No') . "\n";
    echo "  - Service Available: " . ($status['service_available'] ? 'Yes' : 'No') . "\n";
    echo "  - Total Subscriptions: " . $status['total_subscriptions'] . "\n\n";
    
    if (!$status['vapid_configured']) {
        throw new Exception('VAPID keys not configured. Please run config/generate_vapid_keys.php');
    }
    
    if (!$status['service_available']) {
        throw new Exception('Web Push service not available. Please install minishlink/web-push');
    }
    
    if ($status['total_subscriptions'] === 0) {
        echo "No active subscriptions found. Exiting.\n";
        logError('No active subscriptions found for daily notifications');
        exit(0);
    }
    
    // Send daily notifications
    echo "Sending daily notifications...\n";
    $stats = $notificationService->sendDailyNotifications();
    
    // Display results
    echo "\nNotification Statistics:\n";
    echo "  - Total Users: " . $stats['total_users'] . "\n";
    echo "  - Users with Reminders: " . $stats['users_with_reminders'] . "\n";
    echo "  - Notifications Sent: " . $stats['notifications_sent'] . "\n";
    echo "  - Notifications Failed: " . $stats['notifications_failed'] . "\n";
    echo "  - Users without Subscriptions: " . $stats['users_without_subscriptions'] . "\n";
    
    if (isset($stats['error'])) {
        echo "  - Error: " . $stats['error'] . "\n";
    }
    
    // Log results
    $resultMessage = "Daily notifications completed successfully\n";
    $resultMessage .= "Sent: {$stats['notifications_sent']}, Failed: {$stats['notifications_failed']}\n";
    logError($resultMessage, $stats);
    
    echo "\n" . str_repeat('=', 80) . "\n";
    echo "Cron Job Completed Successfully\n";
    echo str_repeat('=', 80) . "\n";
    
    exit(0);
    
} catch (Exception $e) {
    $errorMessage = "Cron job failed: " . $e->getMessage() . "\n";
    $errorMessage .= "Stack trace: " . $e->getTraceAsString() . "\n";
    
    echo "\nERROR: " . $errorMessage;
    logError($errorMessage);
    
    echo "\n" . str_repeat('=', 80) . "\n";
    echo "Cron Job Failed\n";
    echo str_repeat('=', 80) . "\n";
    
    exit(1);
}
