import { NextResponse } from "next/server";
import { getToken } from "@/lib/session";

const API_URL = process.env.NEXT_PUBLIC_API_URL;

/** Thin proxy to Laravel PUT /admin/events/{id}. */
export async function PUT(request: Request, { params }: { params: Promise<{ id: string }> }) {
  const token = await getToken();

  if (!token) {
    return NextResponse.json({ message: "You must log in first." }, { status: 401 });
  }

  const { id } = await params;
  const body = await request.json();

  const apiRes = await fetch(`${API_URL}/admin/events/${id}`, {
    method: "PUT",
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(body),
  });

  const data = await apiRes.json();

  return NextResponse.json(data, { status: apiRes.status });
}

/** Thin proxy to Laravel DELETE /admin/events/{id}. */
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
  const token = await getToken();

  if (!token) {
    return NextResponse.json({ message: "You must log in first." }, { status: 401 });
  }

  const { id } = await params;

  const apiRes = await fetch(`${API_URL}/admin/events/${id}`, {
    method: "DELETE",
    headers: {
      Accept: "application/json",
      Authorization: `Bearer ${token}`,
    },
  });

  const data = await apiRes.json();

  return NextResponse.json(data, { status: apiRes.status });
}
