<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once './core/app/config/Database.php';
include_once './core/app/config/Users.php';

$database = new Database();
$db = $database->getConnection();

$items = new Users($db);

if (isset($_GET['phone'])) {
    $items->id = $_GET['phone'];
    $result = $items->get();

    if ($result->num_rows > 0) {
        $itemRecords = array();
        $itemRecords["data"] = array();
        while ($item = $result->fetch_assoc()) {
            $itemDetails = array(
                "id" => $item['id'],
                "username" => $item['username'],
                "name" => $item['name'],
                "phone" => $item['phone'],
                "is_admin" => $item['is_admin'],
                "is_active" => $item['is_active'],
                "type" => $item['type']
            );
            array_push($itemRecords["data"], $itemDetails);
        }
        http_response_code(200);
        echo json_encode($itemRecords);
    } else {
        http_response_code(404);
        echo json_encode(array("message" => "User not found.", "data" => array()));
    }
} else {
    http_response_code(400);
    echo json_encode(array("message" => "Phone parameter required."));
}
