import { describe, it, expect } from "vitest";
import {
  RecordingTracer,
  RecordingLogger,
} from "@repo/core-testing/instrumentation";
import { MockMediaRepository } from "@/infrastructure/repositories/media.repository.mock";
import type { Media } from "@/entities/models/media";

const SAMPLE_MEDIA: Media = {
  id: "m1",
  alt: "Test image",
  url: "/test/image.jpg",
  filename: "image.jpg",
  mimeType: "image/jpeg",
  filesize: 1024,
};

// Mock repo also wraps in spans.
describe("MockMediaRepository emits spans", () => {
  it("getMedia emits one span with op='repository' and found attribute", async () => {
    const tracer = new RecordingTracer();
    const logger = new RecordingLogger();
    const repo = new MockMediaRepository(tracer, logger);
    await repo._store(SAMPLE_MEDIA);
    await repo.getMedia("m1");
    expect(tracer.spans).toHaveLength(1);
    const getSpan = tracer.findSpan("media.getMedia");
    expect(getSpan).toBeDefined();
    expect(getSpan).toMatchObject({ name: "media.getMedia", op: "repository" });
    expect(getSpan!.attributes.found).toBe(true);
  });

  it("listMedia emits a span with count attribute", async () => {
    const tracer = new RecordingTracer();
    const repo = new MockMediaRepository(tracer);
    await repo._store(SAMPLE_MEDIA);
    tracer.reset();
    await repo.listMedia({ limit: 10 });
    expect(tracer.findSpan("media.listMedia")).toBeDefined();
    expect(tracer.findSpan("media.listMedia")!.attributes.limit).toBe(10);
    expect(tracer.findSpan("media.listMedia")!.attributes.count).toBe(1);
  });

  it("deleteMedia emits a span with deleted=true", async () => {
    const tracer = new RecordingTracer();
    const repo = new MockMediaRepository(tracer);
    await repo._store(SAMPLE_MEDIA);
    tracer.reset();
    await repo.deleteMedia("m1");
    expect(tracer.findSpan("media.deleteMedia")).toBeDefined();
    expect(tracer.findSpan("media.deleteMedia")!.attributes.deleted).toBe(true);
    expect(tracer.findSpan("media.deleteMedia")!.attributes.id).toBe("m1");
  });
});
