<?php
require_once 'config.php';
require_once 'includes/Database.php';

$db = new Database();

echo "<h2>Veritabanı Debug - Gelen Aramalar</h2>";

// Tüm kayıtları göster
$allRecords = $db->fetchAll("SELECT * FROM incoming_calls ORDER BY call_date DESC LIMIT 20");

echo "<h3>Son 20 Kayıt:</h3>";
echo "<p>Toplam kayıt: " . count($allRecords) . "</p>";

if (empty($allRecords)) {
    echo "<p style='color: red;'>❌ Hiç kayıt yok!</p>";
} else {
    echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
    echo "<tr>
            <th>ID</th>
            <th>User ID</th>
            <th>Call Date</th>
            <th>Extension</th>
            <th>Call Count</th>
            <th>Answered</th>
            <th>Unanswered</th>
            <th>Total Duration</th>
          </tr>";

    foreach ($allRecords as $record) {
        echo "<tr>";
        echo "<td>" . $record['id'] . "</td>";
        echo "<td>" . $record['user_id'] . "</td>";
        echo "<td><strong>" . $record['call_date'] . "</strong></td>";
        echo "<td>" . $record['extension'] . "</td>";
        echo "<td>" . $record['call_count'] . "</td>";
        echo "<td>" . $record['answered_count'] . "</td>";
        echo "<td>" . $record['unanswered_count'] . "</td>";
        echo "<td>" . $record['total_duration'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

// Kullanıcıları göster
echo "<h3>Kullanıcılar:</h3>";
$users = $db->fetchAll("SELECT id, username, full_name, extension FROM users");
echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
echo "<tr><th>ID</th><th>Username</th><th>Full Name</th><th>Extension</th></tr>";
foreach ($users as $user) {
    echo "<tr>";
    echo "<td>" . $user['id'] . "</td>";
    echo "<td>" . $user['username'] . "</td>";
    echo "<td>" . $user['full_name'] . "</td>";
    echo "<td>" . $user['extension'] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Test sorgusu - analiz sayfasındaki gibi
echo "<h3>Test Sorgusu (Bu ay):</h3>";
$startDate = date('Y-m-01');
$endDate = date('Y-m-d');
echo "<p>Tarih aralığı: <strong>$startDate</strong> - <strong>$endDate</strong></p>";

$testStats = $db->fetchAll("
    SELECT
        u.id,
        u.username,
        u.full_name,
        u.extension,
        COUNT(ic.id) as record_count,
        SUM(ic.call_count) as total_calls
    FROM users u
    LEFT JOIN incoming_calls ic ON u.id = ic.user_id
        AND ic.call_date BETWEEN ? AND ?
    WHERE u.is_active = 1
    GROUP BY u.id
    HAVING total_calls > 0
", [$startDate, $endDate]);

echo "<p>Bulunan kayıt sayısı: " . count($testStats) . "</p>";

if (!empty($testStats)) {
    echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
    echo "<tr><th>Username</th><th>Full Name</th><th>Extension</th><th>Record Count</th><th>Total Calls</th></tr>";
    foreach ($testStats as $stat) {
        echo "<tr>";
        echo "<td>" . $stat['username'] . "</td>";
        echo "<td>" . $stat['full_name'] . "</td>";
        echo "<td>" . $stat['extension'] . "</td>";
        echo "<td>" . $stat['record_count'] . "</td>";
        echo "<td>" . $stat['total_calls'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>
