import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { prisma } from "@/lib/prisma";

const updateSchema = z.object({
  name: z.string().min(1).max(200).optional(),
  description: z.string().min(1).max(2000).optional(),
  durationMinutes: z.coerce.number().int().min(5).max(600).optional(),
  priceCents: z.coerce.number().int().min(0).optional(),
  active: z.boolean().optional(),
});

export async function PUT(
  request: NextRequest,
  ctx: RouteContext<"/api/admin/services/[id]">
) {
  const { id } = await ctx.params;
  const body = await request.json().catch(() => null);
  const parsed = updateSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json({ error: "Invalid service" }, { status: 400 });
  }
  const service = await prisma.service.update({ where: { id }, data: parsed.data });
  return NextResponse.json({ service });
}

export async function DELETE(
  _request: NextRequest,
  ctx: RouteContext<"/api/admin/services/[id]">
) {
  const { id } = await ctx.params;
  await prisma.service.delete({ where: { id } });
  return NextResponse.json({ success: true });
}
