<?php
require_once("connection_sql.php");

if (isset($_POST['start_date']) && isset($_POST['end_date']) && isset($_POST['user_name'])) {
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $user_name = $_POST['user_name'];

    $sql = "SELECT latitude, longitude, user_name, created_at FROM s_locations 
            WHERE user_name = :user_name 
            AND created_at BETWEEN :start_date AND :end_date
            ORDER BY created_at ASC";

    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':user_name', $user_name);
    $stmt->bindParam(':start_date', $start_date);
    $stmt->bindParam(':end_date', $end_date);
    $stmt->execute();

    $locations = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($locations) {
        echo json_encode(["status" => "success", "data" => $locations]);
    } else {
        echo json_encode(["status" => "error", "message" => "No locations found."]);
    }
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request parameters."]);
}
?>
