import { NextResponse } from "next/server";
import { unlink } from "node:fs/promises";
import path from "node:path";
import { prisma } from "@/lib/prisma";

export async function DELETE(
  _request: Request,
  ctx: RouteContext<"/api/admin/gallery/[id]">
) {
  const { id } = await ctx.params;

  const image = await prisma.galleryImage.findUnique({ where: { id } });
  if (!image) {
    return NextResponse.json({ error: "Not found" }, { status: 404 });
  }

  await prisma.galleryImage.delete({ where: { id } });

  const filePath = path.join(process.cwd(), "public", image.url);
  await unlink(filePath).catch(() => {});

  return NextResponse.json({ success: true });
}
