import { z } from "zod";

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

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

// No output schema — use case returns void.

// ── Use case ─────────────────────────────────────────────────────────────
export type IDeleteMediaUseCase = ReturnType<typeof deleteMediaUseCase>;

export const deleteMediaUseCase =
  (mediaRepository: IMediaRepository) =>
  async (input: DeleteMediaInput): Promise<void> => {
    const existing = await mediaRepository.getMedia(input.id);
    if (!existing) {
      throw new MediaNotFoundError(`Media with id "${input.id}" not found`);
    }
    await mediaRepository.deleteMedia(input.id);
  };
