/**
 * Seed Script: Tags
 *
 * Upserts craft and cooperative tags by slug (idempotent).
 * Safe to run multiple times.
 */

import "dotenv/config";
import mongoose from "mongoose";
import Tag from "../server/models/tag.js";
import { connectDB } from "../server/utils/mongoose.js";

// Convert a slug like "qa-and-testing" to "QA and Testing"
// Special-cases common abbreviations.
const ABBREVIATIONS = new Map([
  ["qa", "QA"],
  ["ux", "UX"],
  ["ui", "UI"],
  ["devops", "DevOps"],
]);

function slugToLabel(slug) {
  return slug
    .split("-")
    .map(
      (word) =>
        ABBREVIATIONS.get(word) ?? word.charAt(0).toUpperCase() + word.slice(1),
    )
    .join(" ");
}

const CRAFT_SLUGS = [
  "game-design",
  "programming",
  "narrative-design",
  "art-and-animation",
  "audio-and-music",
  "production-management",
  "qa-and-testing",
  "community-management",
  "marketing-and-comms",
  "ux-and-ui-design",
  "business-development",
  "devops-and-tools",
  "localization",
  "accessibility",
  "analytics-and-data",
  "education-and-mentoring",
];

const COOPERATIVE_SLUGS = [
  "governance",
  "finance-and-budgeting",
  "legal-structures",
  "conflict-resolution",
  "consensus-decision-making",
  "revenue-sharing",
  "cooperative-bylaws",
  "member-onboarding",
  "democratic-management",
  "worker-ownership",
  "platform-cooperativism",
  "cooperative-marketing",
  "shared-resources",
  "cooperative-funding",
  "community-building",
  "equity-and-inclusion",
  "cooperative-tech",
  "sustainability",
  "collective-bargaining",
  "inter-coop-collaboration",
];

async function seedTags() {
  await connectDB();

  const tagDefs = [
    ...CRAFT_SLUGS.map((slug) => ({
      slug,
      pool: "craft",
      label: slugToLabel(slug),
    })),
    ...COOPERATIVE_SLUGS.map((slug) => ({
      slug,
      pool: "cooperative",
      label: slugToLabel(slug),
    })),
  ];

  let upserted = 0;
  let unchanged = 0;

  for (const { slug, pool, label } of tagDefs) {
    const result = await Tag.updateOne(
      { slug },
      {
        $setOnInsert: {
          slug,
          pool,
          label,
          active: true,
          createdAt: new Date(),
        },
      },
      { upsert: true },
    );
    if (result.upsertedCount > 0) {
      console.log(`  + Created [${pool}] ${label} (${slug})`);
      upserted++;
    } else {
      unchanged++;
    }
  }

  console.log("\n=== Seed Complete ===");
  console.log(`  Total tags defined: ${tagDefs.length}`);
  console.log(`  Newly created:      ${upserted}`);
  console.log(`  Already existed:    ${unchanged}`);
}

seedTags()
  .then(() => {
    console.log("\nTag seed completed successfully");
    process.exit(0);
  })
  .catch((err) => {
    console.error("\nTag seed failed:", err);
    process.exit(1);
  })
  .finally(() => {
    mongoose.connection.close();
  });
