import { db } from "./firebase-config.js";

import {
  collection,
  addDoc,
  serverTimestamp
} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";

export async function createNotification({
  organisationId,
  userId = "",
  playerId = "",
  playerName = "",
  teamId = "",
  teamName = "",
  type = "info",
  title,
  message,
  link = "",
  severity = "info",
  uniqueKey = ""
}) {
  if (!organisationId || !title || !message) {
    console.warn("Notification not created. Missing required fields.");
    return {
      created: false,
      reason: "missing-fields"
    };
  }

  const notificationRef = await addDoc(collection(db, "notifications"), {
    organisationId,
    userId,
    playerId,
    playerName,
    teamId,
    teamName,
    type,
    title,
    message,
    link,
    severity,
    uniqueKey,

    isRead: false,
    readBy: {},

    createdAt: serverTimestamp(),
    updatedAt: serverTimestamp()
  });

  return {
    created: true,
    id: notificationRef.id
  };
}