<?php
// search_clients.php
require_once('../config/dbConnection.php');
header('Content-Type: application/json');

if (!isset($_GET['q']) || strlen($_GET['q']) < 3) {
    echo json_encode([]);
    exit;
}

$query = '%' . $_GET['q'] . '%';
$db = DatabaseConnection::getInstance();
$conn = $db->getConnection();

$sql = "SELECT id, ClientName, BusinessName, MobilePhone, NRC, email
        FROM clients
        WHERE ClientName LIKE ? OR MobilePhone LIKE ? OR NRC LIKE ? OR email LIKE ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssss', $query, $query, $query, $query);
$stmt->execute();

$result = $stmt->get_result();
$clients = [];

while ($row = $result->fetch_assoc()) {
    $clients[] = $row;
}

echo json_encode($clients);
