<?php
declare(strict_types=1);

require_once __DIR__ . '/db_pdo.php';
require_once __DIR__ . '/spatial_helpers.php';

$sql = 'SELECT id, nama_jalan, status, panjang_meter, ST_AsGeoJSON(geom) AS geometry_geojson, created_at, updated_at FROM jalan ORDER BY id DESC';
$stmt = $pdo->query($sql);
$rows = $stmt->fetchAll();

$data = [];
foreach ($rows as $row) {
    $row['id'] = (int) $row['id'];
    $row['panjang_meter'] = (float) $row['panjang_meter'];
    $row['geometry_geojson'] = json_decode((string) $row['geometry_geojson'], true);
    $data[] = $row;
}

json_response(200, [
    'success' => true,
    'data' => $data,
]);
