import { getSession } from "../../../../lib/auth/session";
import { getPrisma } from "../../../../lib/db";
import { ok, fail } from "../../../../lib/http";

export const runtime = "nodejs";

export async function GET() {
  const session = await getSession();
  if (!session) return fail(new Error("Unauthorized"), { status: 401 });

  const prisma = await getPrisma();
  if (!prisma) return fail(new Error("Database not configured"), { status: 503 });

  const registrations = await prisma.webinarRegistration.findMany({
    where: { userId: session.sub },
    include: {
      webinar: true,
    },
    orderBy: { createdAt: "desc" },
  });

  return ok({
    registrations: registrations.map((reg) => ({
      id: reg.id,
      userId: reg.userId,
      webinarId: reg.webinarId,
      registeredAt: reg.createdAt,
      webinar: {
        id: reg.webinar.id,
        title: reg.webinar.title,
        description: reg.webinar.description,
        startAt: reg.webinar.startAt,
        duration: reg.webinar.duration,
        speaker: reg.webinar.speaker,
        priceCents: reg.webinar.priceCents,
        category: reg.webinar.category,
        bannerUrl: reg.webinar.bannerUrl,
        capacity: reg.webinar.capacity,
        visibility: reg.webinar.visibility,
        isActive: reg.webinar.isActive,
      },
    })),
  });
}
