import { db } from "./client";
import { tierDefinitions, ingredients } from "./schema";

// Starter set of canonical ingredients + common EN/FR synonyms, so pantry
// items and recipe ingredients written differently ("sel", "sel fin", "sel
// de table", "table salt") can still be recognized as the same thing (see
// lib/ingredient-match.ts). Deliberately small — grows over time as gaps
// are found, not meant to be exhaustive on day one.
const STAPLE_INGREDIENTS: { name: string; aliases: string[]; category: string }[] = [
  { name: "salt", aliases: ["sel", "sel fin", "sel de table", "table salt", "fine salt", "sea salt", "sel de mer"], category: "spicesCondiments" },
  { name: "sugar", aliases: ["sucre", "sucre blanc", "white sugar", "granulated sugar", "sucre en poudre"], category: "pantry" },
  { name: "black pepper", aliases: ["pepper", "poivre", "poivre noir", "ground pepper", "poivre moulu"], category: "spicesCondiments" },
  { name: "flour", aliases: ["farine", "all-purpose flour", "farine de blé", "plain flour", "wheat flour"], category: "pantry" },
  { name: "butter", aliases: ["beurre", "unsalted butter", "beurre doux", "salted butter", "beurre demi-sel"], category: "dairyEggs" },
  { name: "milk", aliases: ["lait", "whole milk", "lait entier", "lait demi-écrémé"], category: "dairyEggs" },
  { name: "egg", aliases: ["eggs", "œuf", "oeuf", "œufs", "oeufs"], category: "dairyEggs" },
  { name: "onion", aliases: ["oignon", "oignons", "yellow onion", "onions"], category: "produce" },
  { name: "garlic", aliases: ["ail", "garlic clove", "gousse d'ail", "garlic cloves"], category: "produce" },
  { name: "olive oil", aliases: ["huile d'olive", "extra virgin olive oil", "huile d'olive vierge extra"], category: "pantry" },
];

async function seed() {
  console.log("Seeding tier definitions...");
  await db
    .insert(tierDefinitions)
    .values([
      {
        tier: "free",
        maxRecipes: 50,
        aiCallsPerMonth: 10,
        storageMb: 500,
        maxPublicRecipes: 5,
      },
      {
        tier: "pro",
        maxRecipes: 99999,
        aiCallsPerMonth: 500,
        storageMb: 10000,
        maxPublicRecipes: 99999,
      },
      {
        tier: "family",
        // -1 is the actual "no cap" sentinel (see lib/tiers.ts UNLIMITED) —
        // pro uses large-but-finite numbers instead; family is genuinely unlimited.
        maxRecipes: -1,
        aiCallsPerMonth: 2000,
        storageMb: 50000,
        maxPublicRecipes: -1,
      },
    ])
    .onConflictDoNothing();

  console.log("Seeding staple ingredients (name/alias matching)...");
  await db
    .insert(ingredients)
    .values(
      STAPLE_INGREDIENTS.map((i) => ({
        id: crypto.randomUUID(),
        name: i.name,
        aliases: i.aliases,
        category: i.category,
      }))
    )
    .onConflictDoNothing({ target: ingredients.name });

  console.log("Seed complete.");
  process.exit(0);
}

seed().catch((err) => {
  console.error(err);
  process.exit(1);
});
