import { InputParseError } from "../../entities/errors/common";
import {
  getMediaInputSchema,
  type GetMediaOutput,
  type IGetMediaUseCase,
} from "../../application/use-cases/get-media.use-case";

function presenter(value: GetMediaOutput) {
  return value;
}

export type IGetMediaController = ReturnType<typeof getMediaController>;

export const getMediaController =
  (getMediaUseCase: IGetMediaUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = getMediaInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid get-media input", { cause: parsed.error });
    }
    const result = await getMediaUseCase(parsed.data);
    return presenter(result);
  };
