import { NextResponse } from "next/server";
import { sql } from "@/db";
import { requireFamily } from "@/lib/auth";

export async function DELETE(
  request: Request,
  { params }: { params: Promise<{ key: string }> }
) {
  const auth = await requireFamily();
  if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status });

  const familyId = auth.session!.familyId!;
  const { searchParams } = new URL(request.url);
  const childId = searchParams.get("childId");
  if (!childId) return NextResponse.json({ error: "childId required" }, { status: 400 });

  const { key } = await params;

  await sql`
    DELETE FROM milestone_achievements
    WHERE child_id = ${childId} AND family_id = ${familyId} AND milestone_key = ${key}
  `;

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