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;
  }>;
};

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

export async function GET(request: NextRequest, context: Params) {
  return proxyContactsRequest(request, context);
}

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

  if (originError) {
    return originError;
  }

  return proxyContactsRequest(request, context);
}
