import { z } from "zod";

import { MediaNotFoundError } from "../../entities/errors/media";
import { mediaSchema } from "../../entities/models/media";
import type { IMediaRepository } from "../repositories/media.repository.interface";

// ── Input ────────────────────────────────────────────────────────────────
export const getMediaInputSchema = z.object({ id: z.string().min(1) }).strict();
export type GetMediaInput = z.infer<typeof getMediaInputSchema>;

// ── Output ───────────────────────────────────────────────────────────────
export const getMediaOutputSchema = mediaSchema;
export type GetMediaOutput = z.infer<typeof getMediaOutputSchema>;

// ── Use case ─────────────────────────────────────────────────────────────
export type IGetMediaUseCase = ReturnType<typeof getMediaUseCase>;

export const getMediaUseCase =
  (mediaRepository: IMediaRepository) =>
  async (input: GetMediaInput): Promise<GetMediaOutput> => {
    const media = await mediaRepository.getMedia(input.id);
    if (!media) {
      throw new MediaNotFoundError(`Media with id "${input.id}" not found`);
    }
    return getMediaOutputSchema.parse(media);
  };
