import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { z } from "zod";
import { ApiBatchResultsPresenter } from "~/presenters/v3/ApiBatchResultsPresenter.server";
import { ApiRunResultPresenter } from "~/presenters/v3/ApiRunResultPresenter.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";

const ParamsSchema = z.object({
  /* This is the batch friendly ID */
  batchParam: z.string(),
});

export async function loader({ request, params }: LoaderFunctionArgs) {
  // Authenticate the request
  const authenticationResult = await authenticateApiRequest(request);

  if (!authenticationResult) {
    return json({ error: "Invalid or Missing API Key" }, { status: 401 });
  }

  const parsed = ParamsSchema.safeParse(params);

  if (!parsed.success) {
    return json({ error: "Invalid or missing run ID" }, { status: 400 });
  }

  const { batchParam } = parsed.data;

  try {
    const presenter = new ApiBatchResultsPresenter();
    const result = await presenter.call(batchParam, authenticationResult.environment);

    if (!result) {
      return json({ error: "Batch not found" }, { status: 404 });
    }

    return json(result);
  } catch (error) {
    if (error instanceof Error) {
      return json({ error: error.message }, { status: 500 });
    } else {
      return json({ error: JSON.stringify(error) }, { status: 500 });
    }
  }
}
