<?php
session_start();

header("Access-Control-Allow-Origin: https://enerliths.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}
include("../config/db.php");


$user_id = $_SESSION['user_id'];

$sql = "SELECT * FROM chat_messages WHERE user_id=? ORDER BY id ASC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i",$user_id);
$stmt->execute();

$result = $stmt->get_result();

$messages=[];

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

echo json_encode($messages);