import { it, expect, beforeEach, describe } from "vitest";
import { defineContractSuite } from "@repo/core-testing/contract";
import type { IUsersRepository } from "../application/repositories/users.repository.interface.js";
import { userFactory } from "../__factories__/user.factory.js";

export const usersRepositoryContract =
  defineContractSuite<IUsersRepository>(
    "IUsersRepository",
    ({ buildSubject, getTracer }) => {
      let repo: IUsersRepository;

      beforeEach(async () => {
        userFactory.reset();
        repo = await buildSubject();
      });

      // --- getUser ---

      it("createUser then getUser returns it by id", async () => {
        const seed = userFactory.build();
        await repo.createUser(seed);
        const result = await repo.getUser(seed.id);
        expect(result?.id).toBe(seed.id);
        expect(result?.username).toBe(seed.username);
      });

      it("getUser returns undefined for missing id", async () => {
        expect(await repo.getUser("does-not-exist")).toBeUndefined();
      });

      // --- getUserByUsername ---

      it("createUser then getUserByUsername returns it by username", async () => {
        const seed = userFactory.build({ username: "alice" });
        await repo.createUser(seed);
        const result = await repo.getUserByUsername("alice");
        expect(result?.id).toBe(seed.id);
        expect(result?.username).toBe("alice");
      });

      it("getUserByUsername returns undefined for missing username", async () => {
        expect(
          await repo.getUserByUsername("no-such-user"),
        ).toBeUndefined();
      });

      // --- createUser ---

      it("createUser returns the created user", async () => {
        const seed = userFactory.build({ username: "carol" });
        const created = await repo.createUser(seed);
        expect(created.id).toBe(seed.id);
        expect(created.username).toBe("carol");
      });

      describe("span emission (R50)", () => {
        it("getUser emits users.getUser span with id attribute", async () => {
          if (!getTracer) return;
          const tracer = getTracer();
          tracer.reset();
          await repo.getUser("nonexistent");
          const span = tracer.findSpan("users.getUser");
          expect(span).toBeDefined();
          expect(span!.op).toBe("repository");
          expect(span!.attributes.id).toBe("nonexistent");
        });

        it("getUserByUsername emits users.getUserByUsername span", async () => {
          if (!getTracer) return;
          const tracer = getTracer();
          tracer.reset();
          await repo.getUserByUsername("alice");
          const span = tracer.findSpan("users.getUserByUsername");
          expect(span).toBeDefined();
          expect(span!.op).toBe("repository");
        });

        it("createUser emits users.createUser span with id attribute", async () => {
          if (!getTracer) return;
          const tracer = getTracer();
          tracer.reset();
          const seed = userFactory.build({ username: "span-test-user" });
          await repo.createUser(seed);
          const span = tracer.findSpan("users.createUser");
          expect(span).toBeDefined();
          expect(span!.op).toBe("repository");
          expect(span!.attributes.id).toBe(seed.id);
        });
      });
    },
  );
