// One-time seed: populates the `occasions` collection from the static
// OCCASIONS list that currently lives hardcoded in the app. Safe to re-run —
// existing keys are left untouched (only missing ones are inserted), so an
// admin who's since customized title/autoNote/icon via the API won't have
// their edits clobbered by rerunning this.
import "../config/env";
import mongoose from "mongoose";
import { databaseConnect, databaseDisconnect } from "../database/functions";

const OCCASIONS = [
  {
    key: "birthday",
    title: "Birthday",
    autoNote: "Wishing you a fantastic birthday filled with happiness and love! 🎉",
    sortOrder: 1,
  },
  {
    key: "anniversary",
    title: "Anniversary",
    autoNote:
      "Thinking of all the wonderful memories you've created together. Happy Anniversary! 💕",
    sortOrder: 2,
  },
  {
    key: "wedding",
    title: "Wedding",
    autoNote: "Congratulations on your special day! Wishing you a lifetime of love and happiness. 💍",
    sortOrder: 3,
  },
  {
    key: "thankyou",
    title: "Thank You",
    autoNote: "Thank you so much for your kindness and support! 🎁",
    sortOrder: 4,
  },
  {
    key: "valentines",
    title: "Valentine's Day",
    autoNote: "Wishing you a day filled with love, laughter and happiness. 💖",
    sortOrder: 5,
  },
  {
    key: "mothersday",
    title: "Mother's Day",
    autoNote: "Happy Mother's Day! Wishing you all the love you deserve. 🌸",
    sortOrder: 6,
  },
  {
    key: "fathersday",
    title: "Father's Day",
    autoNote: "Happy Father's Day! Thank you for always being there. 🧔",
    sortOrder: 7,
  },
  {
    key: "christmas",
    title: "Christmas",
    autoNote: "Season's Greetings and Merry Christmas! 🎄",
    sortOrder: 8,
  },
  {
    key: "other",
    title: "Other",
    autoNote: "",
    sortOrder: 9,
  },
];

async function seed() {
  await databaseConnect();
  console.log("Connected to MongoDB");

  const occasions = mongoose.connection.collection("occasions");

  const existingKeys = new Set(
    (await occasions.find({}, { projection: { key: 1 } }).toArray()).map(
      (o: any) => o.key,
    ),
  );

  const toInsert = OCCASIONS.filter((o) => !existingKeys.has(o.key)).map((o) => ({
    ...o,
    icon: "",
    isActive: true,
    createdAt: new Date(),
    updatedAt: new Date(),
  }));

  if (toInsert.length === 0) {
    console.log("All occasions already seeded. Nothing to insert.");
  } else {
    const result = await occasions.insertMany(toInsert);
    console.log(`Inserted ${result.insertedCount} occasions:`, toInsert.map((o) => o.key));
  }

  console.log(
    "NOTE: icon is empty for all seeded occasions — upload real icon images via " +
      "POST/PUT /api/v1/occasions (multipart field \"icon\") before relying on them in the UI.",
  );

  await databaseDisconnect();
}

seed().catch((err) => {
  console.error("Seed failed:", err);
  process.exit(1);
});
