<?php

require_once '../../core/api_bootstrap.php';
require_once '../../core/provider_auth.php';

$provider_id = ProviderAuth::id();

try {

    $db = new Database();
    $conn = $db->connect();

    $query = "

SELECT

ld.id,
l.name,
l.phone,
c.name AS category,
ld.sent_at

FROM leads_distribution ld

JOIN customer_leads l
ON ld.lead_id = l.id

LEFT JOIN categories c
ON l.category_id = c.id

WHERE ld.provider_id = :provider

ORDER BY ld.sent_at DESC

LIMIT 5

";

    $stmt = $conn->prepare($query);

    $stmt->execute([
        ':provider' => $provider_id
    ]);

    $notifications = $stmt->fetchAll();

    Response::success($notifications);

} catch (Exception $e) {

    Logger::log("system.log", $e->getMessage());

    Response::error("Server error", 500);

}
