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 proxyPublicLinkRequest(request: NextRequest, { params }: Params) {
  const { id } = await params;
  return proxyHermesRequest(request, `/repairs/${id}/public-link`);
}

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

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

  if (originError) {
    return originError;
  }

  return proxyPublicLinkRequest(request, context);
}

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

  if (originError) {
    return originError;
  }

  return proxyPublicLinkRequest(request, context);
}
