import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const countryIcons: Record<string, string> = {
  美国: '/assets/product-center/countries/us.png',
  日本: '/assets/product-center/countries/jp.png',
  墨西哥: '/assets/product-center/countries/mx.png',
  巴西: '/assets/product-center/countries/br.png',
  中东: '/assets/product-center/countries/middle-east.png',
  波兰: '/assets/product-center/countries/pl.png',
  西班牙: '/assets/product-center/countries/es.png',
  德国: '/assets/product-center/countries/de.png',
  意大利: '/assets/product-center/countries/it.png',
  英国: '/assets/product-center/countries/gb.png',
  加拿大: '/assets/product-center/countries/ca.png',
  澳大利亚: '/assets/product-center/countries/au.png',
  韩国: '/assets/product-center/countries/kr.png',
};

const categoryIcons: Record<string, string> = {
  男士服装: '/assets/product-center/categories/men.svg',
  女士服装: '/assets/product-center/categories/women.svg',
  儿童服装: '/assets/product-center/categories/children.svg',
  家居配饰: '/assets/product-center/categories/home.svg',
};

async function updateExistingIcons(
  entries: Record<string, string>,
  update: (name: string, icon: string) => Promise<{ count: number }>,
): Promise<number> {
  let updated = 0;
  for (const [name, icon] of Object.entries(entries)) {
    const result = await update(name, icon);
    updated += result.count;
  }
  return updated;
}

async function main(): Promise<void> {
  const countries = await updateExistingIcons(countryIcons, (countryName, countryIcon) =>
    prisma.country.updateMany({ where: { countryName }, data: { countryIcon } }),
  );
  const categories = await updateExistingIcons(categoryIcons, (categoryName, categoryIcon) =>
    prisma.category.updateMany({
      where: { categoryName, parentCategoryId: null },
      data: { categoryIcon },
    }),
  );
  console.log(`Configured ${countries} countries and ${categories} root categories.`);
}

main()
  .finally(async () => prisma.$disconnect());
