import { describe, it, expect } from "vitest";
import { getMediaController } from "@/interface-adapters/controllers/get-media.controller";
import { getMediaUseCase } from "@/application/use-cases/get-media.use-case";
import { MockMediaRepository } from "@/infrastructure/repositories/media.repository.mock";
import { MediaNotFoundError } from "@/entities/errors/media";
import { InputParseError } from "@/entities/errors/common";
import { mediaFactory } from "@/__factories__/media.factory";

describe("getMediaController", () => {
  it("returns media on valid input", async () => {
    const repo = new MockMediaRepository();
    const seed = mediaFactory.build({ id: "m-1" });
    await repo._store(seed);

    const useCase = getMediaUseCase(repo);
    const controller = getMediaController(useCase);

    const result = await controller({ id: "m-1" });
    expect(result.id).toBe("m-1");
  });

  it("throws InputParseError when id is missing", async () => {
    const repo = new MockMediaRepository();
    const useCase = getMediaUseCase(repo);
    const controller = getMediaController(useCase);

    await expect(controller({})).rejects.toThrow(InputParseError);
  });

  it("throws InputParseError when id is empty string", async () => {
    const repo = new MockMediaRepository();
    const useCase = getMediaUseCase(repo);
    const controller = getMediaController(useCase);

    await expect(controller({ id: "" })).rejects.toThrow(InputParseError);
  });

  it("throws MediaNotFoundError when media does not exist", async () => {
    const repo = new MockMediaRepository();
    const useCase = getMediaUseCase(repo);
    const controller = getMediaController(useCase);

    await expect(controller({ id: "missing" })).rejects.toThrow(MediaNotFoundError);
  });
});
