```php
<?php

require_once '../../core/api_bootstrap.php';
require_once '../../core/spam_protection.php';
$data = Request::post();

try {

Validator::required($data,[
'name',
'phone',
'category_id',
'pincode'
]);

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

/*
Customer data
*/

$name = Helper::sanitize($data['name']);
$phone = $data['phone'];
$whatsapp = $data['whatsapp'] ?? null;
$email = $data['email'] ?? null;

$address = $data['address'] ?? null;

$category_id = $data['category_id'];
$sub_category_id = $data['sub_category_id'] ?? null;

$pincode = $data['pincode'];


/*
Tracking information
*/

$source_page = $_SERVER['HTTP_REFERER'] ?? null;

$user_ip = $_SERVER['REMOTE_ADDR'] ?? null;

$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null;

$device_type = 'desktop';

if(preg_match('/mobile/i',$user_agent)){
$device_type = 'mobile';
}


/*
Optional SEO tracking
*/

$source_service_slug = $data['service_slug'] ?? null;
$source_city_slug = $data['city_slug'] ?? null;


/*
Insert lead
*/

$query="

INSERT INTO customer_leads
(
name,
phone,
whatsapp,
email,
address,
category_id,
sub_category_id,
pincode,

source_page,
source_service_slug,
source_city_slug,

user_ip,
user_agent,
device_type
)

VALUES
(
:name,
:phone,
:whatsapp,
:email,
:address,
:category_id,
:sub_category_id,
:pincode,

:source_page,
:service_slug,
:city_slug,

:user_ip,
:user_agent,
:device_type
)

";

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

$stmt->execute([

':name'=>$name,
':phone'=>$phone,
':whatsapp'=>$whatsapp,
':email'=>$email,
':address'=>$address,
':category_id'=>$category_id,
':sub_category_id'=>$sub_category_id,
':pincode'=>$pincode,

':source_page'=>$source_page,
':service_slug'=>$source_service_slug,
':city_slug'=>$source_city_slug,

':user_ip'=>$user_ip,
':user_agent'=>$user_agent,
':device_type'=>$device_type

]);

$lead_id = $conn->lastInsertId();

/*
Trigger lead distribution
*/

$conn->prepare("

UPDATE customer_leads
SET status='new'
WHERE id=:id

")->execute([

':id'=>$lead_id

]);


/*
Log activity
*/

Logger::activity(
ApiAuth::employeeId(),
"create",
"customer_leads",
$lead_id,
ApiAuth::ip()
);

Response::success([

"lead_id"=>$lead_id

],"Lead created successfully");

}
catch(Exception $e){

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

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

}