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

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    require_once '../config/database.php';
    
    $pdo = getDBConnection();
    
    // Get all appointments data
    $allAppointments = $pdo->query("
        SELECT 
            id, user_name, appointment_date, time_slot, amount, status, consultation_type, created_at
        FROM appointments 
        ORDER BY created_at DESC 
        LIMIT 20
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Get today's data specifically
    $todayAppointments = $pdo->query("
        SELECT 
            id, user_name, appointment_date, time_slot, amount, status, consultation_type, created_at
        FROM appointments 
        WHERE DATE(appointment_date) = CURDATE()
        ORDER BY created_at DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Get today's created appointments
    $todayCreated = $pdo->query("
        SELECT 
            id, user_name, appointment_date, time_slot, amount, status, consultation_type, created_at
        FROM appointments 
        WHERE DATE(created_at) = CURDATE()
        ORDER BY created_at DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Get tatkal bookings - check all possible variations
    $tatkalBookings = $pdo->query("
        SELECT 
            id, user_name, appointment_date, time_slot, amount, status, consultation_type, created_at
        FROM appointments 
        WHERE consultation_type LIKE '%tatkal%' OR consultation_type = 'tatkal_booking'
        ORDER BY created_at DESC
        LIMIT 10
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Get all unique consultation types
    $allConsultationTypes = $pdo->query("
        SELECT DISTINCT consultation_type, COUNT(*) as count
        FROM appointments 
        GROUP BY consultation_type
        ORDER BY count DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Get counts
    $totalCount = $pdo->query("SELECT COUNT(*) FROM appointments")->fetchColumn();
    $todayScheduledCount = $pdo->query("SELECT COUNT(*) FROM appointments WHERE DATE(appointment_date) = CURDATE()")->fetchColumn();
    $todayCreatedCount = $pdo->query("SELECT COUNT(*) FROM appointments WHERE DATE(created_at) = CURDATE()")->fetchColumn();
    $tatkalCount = $pdo->query("SELECT COUNT(*) FROM appointments WHERE consultation_type LIKE '%tatkal%'")->fetchColumn();
    
    echo json_encode([
        'success' => true,
        'current_date' => date('Y-m-d'),
        'current_time' => date('Y-m-d H:i:s'),
        'counts' => [
            'total_appointments' => (int)$totalCount,
            'today_scheduled' => (int)$todayScheduledCount,
            'today_created' => (int)$todayCreatedCount,
            'tatkal_total' => (int)$tatkalCount
        ],
        'all_appointments' => $allAppointments,
        'today_appointments' => $todayAppointments,
        'today_created' => $todayCreated,
        'tatkal_bookings' => $tatkalBookings,
        'all_consultation_types' => $allConsultationTypes
    ]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage(),
        'file' => $e->getFile(),
        'line' => $e->getLine()
    ]);
}
?>
