<?php
require_once 'db_config.php';

/**
 * שליחת הודעה חדשה בצ'אט
 */
function sendMessage($request_id, $sender_id, $text) {
    global $conn;
    $sql = "INSERT INTO messages (request_id, sender_id, message_text) VALUES (?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iis", $request_id, $sender_id, $text);
    return $stmt->execute();
}

/**
 * משיכת כל ההיסטוריה של צ'אט ספציפי
 */
function getChatHistory($request_id) {
    global $conn;
    $messages = [];
    $sql = "SELECT * FROM messages WHERE request_id = ? ORDER BY created_at ASC";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $request_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        $messages[] = $row;
    }
    return $messages;
}
?>