import { prisma } from "@stamp/shared";

const stampData = [
  { name: "尹氏汤包" },
  { name: "中国移动 5G" },
  { name: "紫金农商银行" },
  { name: "孟令军炒货铺" },
  { name: "春山酒窖" },
  { name: "金陵绣男" },
  { name: "LBZ" },
  { name: "二条商店" },
  { name: "陶玉梅" },
  { name: "芳婆糕团" },
  { name: "书锦城创" },
  { name: "闲鱼循环商店" },
  { name: "闽南茶叶店" },
  { name: "魏鬼虾神" },
  { name: "农家小院" },
  { name: "李记清真馆" },
];

async function seed() {
  console.log("Seeding database...");

  // Clear existing stamps (cascades to collections)
  await prisma.stamp.deleteMany();

  const stamps = await Promise.all(
    stampData.map((s, idx) => {
      const pos = String(idx + 1).padStart(2, "0");
      return prisma.stamp.create({
        data: {
          name: s.name,
          imageColor: `/uploads/stamps/stamp-${pos}-color.jpg`,
          imageGrey: `/uploads/stamps/stamp-${pos}-grey.jpg`,
          sortOrder: idx + 1,
        },
      });
    }),
  );

  console.log(`Created ${stamps.length} stamps`);

  // Create redemption rules if none exist
  const existingRules = await prisma.redemptionRule.count();
  if (existingRules === 0) {
    const rules = await Promise.all([
      prisma.redemptionRule.create({
        data: { name: "纪念书签", description: "精美城市纪念书签一枚", threshold: 4, sortOrder: 1 },
      }),
      prisma.redemptionRule.create({
        data: { name: "手绘明信片套装", description: "一套 5 张手绘城市明信片", threshold: 8, sortOrder: 2 },
      }),
      prisma.redemptionRule.create({
        data: { name: "限定徽章礼盒", description: "城市限定版金属徽章礼盒", threshold: 12, sortOrder: 3 },
      }),
      prisma.redemptionRule.create({
        data: { name: "城市记忆礼包", description: "包含纪念 T 恤、帆布袋和城市画册", threshold: 16, sortOrder: 4 },
      }),
    ]);
    console.log(`Created ${rules.length} redemption rules`);
  } else {
    console.log(`Kept existing ${existingRules} redemption rules`);
  }

  console.log("\nStamp IDs for testing:");
  stamps.forEach((s) => {
    console.log(`  ${s.sortOrder}. ${s.name}: /collect/${s.id}`);
  });

  console.log("\nSeed complete!");
}

seed()
  .catch(console.error)
  .finally(() => prisma.$disconnect());
