import { NextRequest } from "next/server";
import { z } from "zod";
import { getPrisma } from "../../../lib/db";
import { getSession } from "../../../lib/auth/session";
import { ok, fail } from "../../../lib/http";

export const runtime = "nodejs";

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

  const session = await getSession();
  const isAdmin = session?.role === "ADMIN";

  const url = new URL(req.url);
  const page = parseInt(url.searchParams.get("page") || "0");
  const limit = parseInt(url.searchParams.get("limit") || "20");
  const offset = page * limit;

  const where = isAdmin ? {} : { visibility: "PUBLIC" as const, isActive: true };

  const [webinars, total] = await Promise.all([
    prisma.webinar.findMany({
      where,
      orderBy: { startAt: "asc" },
      take: limit,
      skip: offset,
    }),
    prisma.webinar.count({ where }),
  ]);

  return ok({ webinars, total, page, limit });
}

const CreateBody = z.object({
  title: z.string().min(3),
  description: z.string().min(1),
  speaker: z.string().min(1),
  startAt: z.string(),
  duration: z.number().int().positive(),
  bannerUrl: z.string().url().optional().or(z.literal("")),
  category: z.string().min(1),
  visibility: z.enum(["PUBLIC", "PRIVATE"]),
  isActive: z.boolean(),
  capacity: z.number().int().positive(),
  priceCents: z.number().int().min(0),
  learningPoints: z.array(z.string()).optional(),
});

export async function POST(req: NextRequest) {
  const session = await getSession();
  if (!session || session.role !== "ADMIN") return fail(new Error("Forbidden"), { status: 403 });

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

  const parsed = CreateBody.safeParse(await req.json().catch(() => ({})));
  if (!parsed.success) return fail(new Error("Invalid input"), { isAdmin: true });

  const w = await prisma.webinar.create({
    data: {
      ...parsed.data,
      bannerUrl: parsed.data.bannerUrl ? parsed.data.bannerUrl : null,
      startAt: new Date(parsed.data.startAt),
      meetingInfo: {},
      learningPoints: parsed.data.learningPoints || [],
    },
  });

  return ok({ webinar: w });
}
