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

export const runtime = "nodejs";

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

export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params;
  const prisma = await getPrisma();
  if (!prisma) {
    return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
  }

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

  try {
    const webinar = await prisma.webinar.findUnique({
      where: { id: id },
      include: {
        _count: {
          select: { registrations: true },
        },
      },
    });

    if (!webinar) {
      return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
    }

    // Check visibility
    if (!isAdmin && (webinar.visibility !== "PUBLIC" || !webinar.isActive)) {
      return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
    }

    return NextResponse.json({ ok: true, webinar });
  } catch (error) {
    console.error("Error fetching webinar:", error);
    return NextResponse.json({ ok: false, message: "Failed to fetch webinar" }, { status: 500 });
  }
}

export async function PATCH(
  req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params;
  const session = await getSession();
  if (!session || session.role !== "ADMIN") {
    return NextResponse.json({ ok: false, message: "Unauthorized" }, { status: 403 });
  }

  const prisma = await getPrisma();
  if (!prisma) {
    return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
  }

  try {
    const body = await req.json();
    const parsed = UpdateBody.safeParse(body);

    if (!parsed.success) {
      return NextResponse.json(
        { ok: false, message: "Invalid input", errors: parsed.error.errors },
        { status: 400 }
      );
    }

    const updateData: any = { ...parsed.data };
    
    if (parsed.data.startAt) {
      updateData.startAt = new Date(parsed.data.startAt);
    }

    if (parsed.data.learningPoints) {
      updateData.learningPoints = parsed.data.learningPoints;
    }

    const webinar = await prisma.webinar.update({
      where: { id: id },
      data: updateData,
    });

    return NextResponse.json({ ok: true, webinar, message: "Webinar updated successfully" });
  } catch (error: any) {
    console.error("Error updating webinar:", error);
    if (error.code === "P2025") {
      return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
    }
    return NextResponse.json({ ok: false, message: "Failed to update webinar" }, { status: 500 });
  }
}

export async function DELETE(
  req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params;
  const session = await getSession();
  if (!session || session.role !== "ADMIN") {
    return NextResponse.json({ ok: false, message: "Unauthorized" }, { status: 403 });
  }

  const prisma = await getPrisma();
  if (!prisma) {
    return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
  }

  try {
    await prisma.webinar.delete({
      where: { id: id },
    });

    return NextResponse.json({ ok: true, message: "Webinar deleted successfully" });
  } catch (error: any) {
    console.error("Error deleting webinar:", error);
    if (error.code === "P2025") {
      return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
    }
    return NextResponse.json({ ok: false, message: "Failed to delete webinar" }, { status: 500 });
  }
}
