import 'dotenv/config';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../src/generated/prisma/client';
import { CDC_TRANSLATIONS, CEFP_TRANSLATIONS, seedTranslationItems } from './seeds/translations';

async function main() {
  const connectionString = process.env.DATABASE_URL;
  if (!connectionString) throw new Error('DATABASE_URL is missing');
  const prisma = new PrismaClient({ adapter: new PrismaPg({ connectionString }) });
  try {
    for (const [module, all] of [['cdc', CDC_TRANSLATIONS], ['cefp', CEFP_TRANSLATIONS]] as const) {
      console.log(await seedTranslationItems(
        prisma, module, all.filter(item => item.page === 'rgc_decision'),
      ));
    }
    // Repair only the incorrect default inserted by the earlier shared CEFP seed.
    // Preserve any title subsequently edited in Admin.
    await prisma.translation.updateMany({
      where: {
        module: 'cdc', page: 'rgc_decision', translationKey: 'export.title',
        englishText: 'CEFP RGC Decisions',
        khmerText: 'សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល (CEFP)',
      },
      data: {
        englishText: 'CDC RGC Decisions',
        khmerText: 'សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល (CDC)',
      },
    });
  } finally {
    await prisma.$disconnect();
  }
}

main().catch(error => {
  console.error('RGC Decision translation seed failed:', error);
  process.exitCode = 1;
});
