<?php
// Filename: cron_daily_summary.php
require 'config.php';

$today = date('Y-m-d');

// 1. Count Total Absentees Today
$abs_stmt = $pdo->prepare("SELECT COUNT(DISTINCT student_id) FROM student_attendance WHERE date = ? AND status = 'absent'");
$abs_stmt->execute([$today]);
$total_absent = $abs_stmt->fetchColumn();

// 2. Count High Severity Discipline Issues
$disc_stmt = $pdo->prepare("SELECT COUNT(*) FROM student_discipline WHERE reported_date = ? AND severity = 'high'");
$disc_stmt->execute([$today]);
$total_high_disc = $disc_stmt->fetchColumn();

// 3. Get Principal User IDs
$principals = $pdo->query("SELECT id FROM users WHERE role LIKE '%principal%'")->fetchAll(PDO::FETCH_COLUMN);

if (!empty($principals)) {
    $title = "📊 Daily School Summary (" . date('d M') . ")";
    $message = "Total Absentees: $total_absent | High-Level Incidents: $total_high_disc";
    
    sendPushNotification($principals, $title, $message, 'admin_dashboard.php');
}

echo "Summary sent to " . count($principals) . " principal(s).";