<?php
require_once __DIR__ . "/../services/db.php";

/**
 * createNotification($conn, $userId, $projectId, $taskId, $type, $message, $data)
 * $data is an associative array that will be json_encoded
 */
function createNotification($conn, $userId, $projectId, $taskId, $type, $message, $data = null) {
    $json = $data ? json_encode($data) : null;
    sqlsrv_query(
        $conn,
        "INSERT INTO notifications (user_id, project_id, task_id, type, message, data) VALUES (?, ?, ?, ?, ?, ?)",
        [$userId, $projectId, $taskId, $type, $message, $json]
    );

    // Optionally return the inserted id (SCOPE_IDENTITY)
    $stmt = sqlsrv_query($conn, "SELECT SCOPE_IDENTITY() AS id");
    $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
    return $row ? $row['id'] : null;
}
