<?php

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

$provider_id = ProviderAuth::id();

$data = Request::post();

try {

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

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

$stmt = $conn->prepare("

UPDATE lead_distribution
SET status = :status
WHERE id = :distribution_id
AND provider_id = :provider_id

");

$stmt->execute([

':status' => $data['status'],
':distribution_id' => $data['distribution_id'],
':provider_id' => $provider_id

]);

if($stmt->rowCount() == 0){

Response::error("Lead not found or not allowed",404);

}

Response::success([],"Lead status updated");

}
catch(Exception $e){

Logger::log("system.log",$e->getMessage());
Response::error("Server error",500);

}