<?php
/**
 * API: جلب فحوصات اليوم
 * GET /api/supervisor/endpoints/get_today_inspections.php
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    if (!$db) {
        throw new Exception("فشل الاتصال بقاعدة البيانات");
    }
    
    $today = date('Y-m-d');
    
    $query = "SELECT * FROM inspection_requests_new 
              WHERE DATE(created_at) = :today OR DATE(appointment_date) = :today
              ORDER BY appointment_time ASC, created_at DESC";
    
    $stmt = $db->prepare($query);
    $stmt->execute([':today' => $today]);
    
    $inspections = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $inspections,
        'date' => $today
    ]);
    
} catch (Exception $e) {
    error_log("Error in get_today_inspections.php: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>