<?php
session_start();
header('Content-Type: application/json');
include '../../config/db.php';

if(!isset($_SESSION['user_id'])){ 
    echo json_encode(['success'=>false]); 
    exit; 
}

$user_id = intval($_GET['user_id'] ?? $_SESSION['user_id']);

$result = $conn->query("
    SELECT cl.*, l.full_name, l.phone
    FROM call_logs cl
    LEFT JOIN leads l ON l.id = cl.lead_id
    WHERE cl.user_id = '$user_id'
    ORDER BY cl.created_at DESC
    LIMIT 50
");

$logs = [];
while($r = $result->fetch_assoc()) $logs[] = $r;

echo json_encode(['success'=>true, 'logs'=>$logs]);