<?php
header('Content-Type: application/json');
include('DB.php');

$bdd = new DB();

$user_id = isset($_GET['user_id']) ? $_GET['user_id'] : null;
$dept    = isset($_GET['dept']) ? $_GET['dept'] : null;

if (empty($user_id) || empty($dept)) {
    echo json_encode(["status" => "idle"]);
    exit;
}

$getMobileRow = $bdd->getOne(
    "SELECT id, mobile_no, followup_id 
     FROM call_customer 
     WHERE user_id = ? AND dept = ? 
     LIMIT 1",
    [$user_id, $dept]
);

if (!empty($getMobileRow)) {

    // delete immediately (as per your requirement)
    $bdd->execute(
        "DELETE FROM call_customer WHERE id = ?",
        [$getMobileRow['id']]
    );

    echo json_encode([
        "status"       => "call",
        "mobile_no"    => $getMobileRow['mobile_no'],
        "followup_id"  => $getMobileRow['followup_id']
    ]);

} else {
    echo json_encode(["status" => "idle"]);
}

$bdd->disconnect();
