/**
 * Reset all shift-planning data for a clean start.
 * Usage: npx ts-node scripts/reset-shift-planning.ts [--month=YYYY-MM]
 *
 * Clears schedules, assignments, approvals, delegations, verifier assignments.
 * Enables current-month shift planning in app_settings.
 */
import 'dotenv/config';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
const SHIFT_CURRENT_MONTH_KEY = 'leaves.shift_current_month_enabled';

async function main() {
  const monthArg = process.argv.find((a) => a.startsWith('--month='))?.split('=')[1];

  console.log('Resetting shift planning data…');

  await prisma.team_shift_approvals.deleteMany({});
  await prisma.team_shift_member_assignments.deleteMany({});
  await prisma.team_shift_schedules.deleteMany({});
  await prisma.team_shift_planner_delegations.deleteMany({});
  await prisma.team_shift_verifier_assignments.deleteMany({});

  await prisma.app_settings.upsert({
    where: { key: SHIFT_CURRENT_MONTH_KEY },
    create: { key: SHIFT_CURRENT_MONTH_KEY, value: 'true', updated_at: new Date() },
    update: { value: 'true', updated_at: new Date() },
  });

  const teams = await prisma.l10_teams.findMany({
    where: { active: true },
    orderBy: { name: 'asc' },
  });

  console.log(`Cleared shift data. Current-month planning: ON. Active teams: ${teams.length}`);
  for (const t of teams) {
    console.log(`  - ${t.name} (${t.id})`);
  }

  if (monthArg) {
    console.log(`\nNo planner/delegation rows seeded. Use HR Shift Setup for ${monthArg}.`);
  } else {
    console.log('\nUse HR → Shift Setup to assign planners and verifiers for the target month.');
  }

  console.log('Done.');
}

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