<?php
session_start();
$db = new mysqli('localhost', 'papino', 'tt1201', 'chatgpt');

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

$data = json_decode(file_get_contents("php://input"), true);
if (isset($data['id'])) {
    $id = $db->real_escape_string($data['id']);
    $user_id = $_SESSION['user_id']; // Ensure that users can only delete their conversations

    $query = "DELETE FROM ai WHERE id = '$id' AND user_id = '$user_id'";
    if ($db->query($query)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'error' => $db->error]);
    }
}
?>