/**
 * Canonicalize trainees.highest_education and trainees.employment_status values
 * to protocol-aligned categories used by eligibility calculations.
 *
 * Usage:
 *   npx tsx scripts/normalize-eligibility-fields.ts
 */
import "dotenv/config";
import { PrismaClient } from "@prisma/client";
import {
  normalizeEmploymentStatus,
  normalizeHighestEducation,
} from "../src/services/traineeEligibility.js";

async function main() {
  const prisma = new PrismaClient();
  try {
    const rows = await prisma.trainee.findMany({
      select: { id: true, highestEducation: true, employmentStatus: true },
    });

    let changed = 0;
    for (const r of rows) {
      const nextEducation = normalizeHighestEducation(r.highestEducation);
      const nextEmployment = normalizeEmploymentStatus(r.employmentStatus);
      const educationChanged = (r.highestEducation ?? null) !== (nextEducation ?? null);
      const employmentChanged = (r.employmentStatus ?? null) !== (nextEmployment ?? null);
      if (!educationChanged && !employmentChanged) continue;
      await prisma.trainee.update({
        where: { id: r.id },
        data: {
          highestEducation: nextEducation,
          employmentStatus: nextEmployment,
        },
      });
      changed += 1;
    }

    console.log(`Eligibility field normalization done. Updated trainees: ${changed}/${rows.length}`);
  } finally {
    await prisma.$disconnect();
  }
}

void main().catch((e) => {
  console.error(e);
  process.exit(1);
});

