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

// Include database and object files
include_once '../config/database.php';
include_once '../objects/createrole.php';

// Instantiate database and object
$database = new Database();
$db = $database->getConnection();
$userrole = new UserRole($db);

// Get all records
$stmt = $userrole->getAllRows();
$num = $stmt->rowCount();

// Check if there are records
if ($num > 0) {
    $arr = ["records" => []];
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        // Cast all fields to int (you can modify this logic if needed)
        $row = array_map(function($value) {
            return is_numeric($value) ? (int)$value : $value;
        }, $row);
        
        // Add the row to the records array
        array_push($arr["records"], $row);
    }

    echo json_encode($arr);
} else {
    // No records found
    echo json_encode(["message" => "No roles found."]);
}
?>
