
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

const categoryImages = {
  rice: 'https://musafoods.ca/wp-content/uploads/2024/05/1-2-300x300.png',
  beans: 'https://musafoods.ca/wp-content/uploads/2024/05/2-3-300x300.png',
  seasonings: 'https://musafoods.ca/wp-content/uploads/2024/05/3-3-300x300.png',
  haitian: 'https://musafoods.ca/wp-content/uploads/2024/05/6-1-300x300.png',
  semolina: 'https://musafoods.ca/wp-content/uploads/2024/05/ICE-1-300x300.png',
  wheat_oat: 'https://musafoods.ca/wp-content/uploads/2024/05/5-2-300x300.png',
  dry_fish: 'https://musafoods.ca/wp-content/uploads/2024/05/7-1-300x300.png',
  corn_meal: 'https://musafoods.ca/wp-content/uploads/2024/05/8-1-300x300.png',
  detergent: 'https://musafoods.ca/wp-content/uploads/2024/05/15-1-300x300.png',
};

const staticCategories = [
  { id: 'rice', nameEn: 'Rice', nameFr: 'Riz' },
  { id: 'beans', nameEn: 'Beans', nameFr: 'Haricots' },
  { id: 'seasonings', nameEn: 'Seasonings', nameFr: 'Assaisonnements' },
  { id: 'haitian', nameEn: 'Haitian Products', nameFr: 'Produits Haitiens' },
  { id: 'semolina', nameEn: 'Semolina', nameFr: 'Semoule' },
  { id: 'wheat_oat', nameEn: 'Wheat & Oat Meal', nameFr: 'Ble et Avoine' },
  { id: 'dry_fish', nameEn: 'Dry Fish', nameFr: 'Poisson Seche' },
  { id: 'corn_meal', nameEn: 'Corn Meal', nameFr: 'Farine de Mais' },
  { id: 'detergent', nameEn: 'Detergent', nameFr: 'Detergent' },
];

async function main() {
  console.log('Updating categories with images and translations...');

  for (const cat of staticCategories) {
    const slug = cat.id;
    const image = categoryImages[slug] || null;

    console.log(`Updating ${slug}...`);

    await prisma.category.upsert({
      where: { slug },
      update: {
        nameEn: cat.nameEn,
        nameFr: cat.nameFr,
        image: image,
      },
      create: {
        slug,
        nameEn: cat.nameEn,
        nameFr: cat.nameFr,
        image: image,
      },
    });
  }
  
  // Also, we need to ensure products are linked.
  // In the previous step, we linked by matching `product.category` string to `category.slug` (or name?).
  // My previous script derived slug from string.
  // "rice" -> "rice".
  // "wheat_oat" -> "wheat_oat".
  // So they should be linked. 
  // Let's double check if any products are unlinked.
  
  const unlinkedCount = await prisma.product.count({
      where: { categoryId: null }
  });
  
  console.log(`Unlinked products count: ${unlinkedCount}`);
  
  if (unlinkedCount > 0) {
      console.log("Attempting to link unlinked products...");
      const categories = await prisma.category.findMany();
      for (const cat of categories) {
          // Link by exact match of slug to concept
          // The product.category field currently holds the ID string from the static list (e.g. "wheat_oat")
          await prisma.product.updateMany({
              where: { 
                  category: cat.slug,
                  categoryId: null
              },
              data: { categoryId: cat.id }
          });
      }
  }

  console.log('Update complete.');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
