import { NextRequest } from "next/server";

import { proxyHermesRequest } from "@/lib/server/hermes-proxy";
import { assertSameOrigin } from "@/lib/server/request-guards";

type Params = {
  params: Promise<{
    id: string;
    contactId: string;
  }>;
};

async function proxyContactRequest(request: NextRequest, { params }: Params) {
  const { id, contactId } = await params;
  return proxyHermesRequest(request, `/customers/${id}/contacts/${contactId}`);
}

export async function PUT(request: NextRequest, context: Params) {
  const originError = assertSameOrigin(request);

  if (originError) {
    return originError;
  }

  return proxyContactRequest(request, context);
}

export async function DELETE(request: NextRequest, context: Params) {
  const originError = assertSameOrigin(request);

  if (originError) {
    return originError;
  }

  return proxyContactRequest(request, context);
}
