<?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',
'action_type'
]);

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

$stmt=$conn->prepare("

INSERT INTO lead_contact_actions
(distribution_id,provider_id,action_type,created_at)

VALUES
(:distribution_id,:provider_id,:action_type,NOW())

");

$stmt->execute([

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

]);

$conn->prepare("

UPDATE lead_distribution
SET status='contacted'
WHERE id=:id
AND provider_id=:provider

")->execute([

':id'=>$data['distribution_id'],
':provider'=>$provider_id

]);

Response::success([], "Activity tracked");

}
catch(Exception $e){

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

}