import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";

// GET /api/learn/certificates/verify/[serial] — public verification
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ serial: string }> }
) {
  try {
    const { serial } = await params;

    const certificate = await prisma.learnCertificate.findUnique({
      where: { serialNumber: serial },
      include: {
        course: {
          select: { title: true },
        },
      },
    });

    if (!certificate) {
      return NextResponse.json({ valid: false });
    }

    return NextResponse.json({
      valid: !certificate.revoked,
      serialNumber: certificate.serialNumber,
      userName: certificate.userName,
      courseTitle: certificate.course.title,
      moduleCount: certificate.moduleCount,
      score: certificate.score,
      issuedAt: certificate.issuedAt.toISOString(),
      revoked: certificate.revoked,
    });
  } catch (error) {
    console.error("GET /api/learn/certificates/verify/[serial] error:", error);
    return NextResponse.json({ valid: false });
  }
}
