const { PrismaClient } = require('../app/generated/prisma');

async function main() {
  const prisma = new PrismaClient();
  const id = process.argv[2];
  if (!id) {
    console.error('Usage: node scripts/check_content_id.js <contentId>');
    process.exit(1);
  }
  const norm = (s) => (s ?? '').trim();
  const idNorm = norm(id);

  const content = await prisma.content.findUnique({ where: { id } });
  const contentNorm = id === idNorm ? content : await prisma.content.findUnique({ where: { id: idNorm } });

  let map = null, mapNorm = null;
  try { map = await prisma.contentHandle.findUnique({ where: { contentId: id } }); } catch {}
  try { if (id !== idNorm) mapNorm = await prisma.contentHandle.findUnique({ where: { contentId: idNorm } }); } catch {}

  const vpEq = await prisma.viewPerDay.count({ where: { contented: id } });
  const vpEqNorm = id === idNorm ? vpEq : await prisma.viewPerDay.count({ where: { contented: idNorm } });
  const vpLike = await prisma.viewPerDay.count({ where: { contented: { contains: id } } });
  const vpLikeNorm = id === idNorm ? vpLike : await prisma.viewPerDay.count({ where: { contented: { contains: idNorm } } });
  const sample = await prisma.viewPerDay.findMany({ where: { contented: { contains: idNorm } }, orderBy: { date: 'desc' }, take: 5 });

  console.log(JSON.stringify({
    input: id,
    normalized: idNorm,
    contentExists: !!content || !!contentNorm,
    contentIdExact: !!content,
    contentIdNormalized: !!contentNorm,
    mapExists: !!map || !!mapNorm,
    mapExact: !!map,
    mapNormalized: !!mapNorm,
    viewCounts: { eq: vpEq, eqNorm: vpEqNorm, like: vpLike, likeNorm: vpLikeNorm },
    sample
  }, null, 2));

  await prisma.$disconnect();
}

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


