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

// DELETE — cancel/revoke a pending invite (only the family that sent it can delete it)
export async function DELETE(
  _req: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const auth = await requireFamily();
  if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status });

  const { id } = await params;

  try {
    const result = await sql.unsafe(
      `DELETE FROM family_invites WHERE id = $1 AND family_id = $2`,
      [id, auth.session!.familyId]
    );
    // result.count === 0 means the invite didn't belong to this family or didn't exist
    return NextResponse.json({ success: true });
  } catch (error) {
    console.error(error);
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
