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

$provider_id = ProviderAuth::id();

$data = Request::get();

try {

    Validator::required($data, ['distribution_id']);

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

    $query = "

SELECT

l.*,
c.name AS category,
sc.name AS sub_category

FROM leads_distribution ld

JOIN customer_leads l
ON ld.lead_id = l.id

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

LEFT JOIN sub_categories sc
ON l.sub_category_id = sc.id

WHERE ld.id=:id

LIMIT 1

";

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

    $stmt->execute([
        ':id' => $data['distribution_id']
    ]);

    $lead = $stmt->fetch();

    Response::success($lead);

} catch (Exception $e) {

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

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

}