/**
 * HR import 시 TaskDetail(피드백)에 잘못 들어간 progressStatus 레거시 삭제
 * — milestoneId 없고 authorName 없는 행 (seed 자동 생성분)
 *
 * npx tsx scripts/cleanup-legacy-task-details.ts
 * npx tsx scripts/cleanup-legacy-task-details.ts --dry-run
 */
import 'dotenv/config';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
const dryRun = process.argv.includes('--dry-run');

async function main() {
  const legacy = await prisma.taskDetail.findMany({
    where: {
      milestoneId: null,
      OR: [{ authorName: null }, { authorName: '' }],
    },
    include: { task: { select: { title: true } } },
    orderBy: { createdAt: 'asc' },
  });

  if (legacy.length === 0) {
    console.log('✅ 삭제할 레거시 피드백 없음');
    return;
  }

  console.log(`레거시 TaskDetail ${legacy.length}건 (일정 미연결 · 작성자 없음):`);
  for (const row of legacy) {
    const preview = row.content.replace(/\s+/g, ' ').slice(0, 72);
    console.log(`  · [${row.task.title}] ${preview}${row.content.length > 72 ? '…' : ''}`);
  }

  if (dryRun) {
    console.log('\n(dry-run — 삭제하지 않음. 적용: npx tsx scripts/cleanup-legacy-task-details.ts)');
    return;
  }

  const result = await prisma.taskDetail.deleteMany({
    where: {
      id: { in: legacy.map((r) => r.id) },
    },
  });

  console.log(`\n✅ ${result.count}건 삭제 완료`);
}

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