import { it, expect, beforeEach, describe } from "vitest";
import { defineContractSuite } from "@repo/core-testing/contract";
import type { ISiteSettingsRepository } from "../application/repositories/site-settings.repository.interface.js";

/**
 * Contract for ISiteSettingsRepository.
 *
 * SiteSettings is a singleton (Payload Global). The interface exposes
 * only getSiteSettings(). The contract verifies the shape of the return value.
 */
export const siteSettingsRepositoryContract =
  defineContractSuite<ISiteSettingsRepository>(
    "ISiteSettingsRepository",
    ({ buildSubject, getTracer }) => {
      let repo: ISiteSettingsRepository;

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

      // --- getSiteSettings ---

      it("getSiteSettings returns an object with a non-empty siteName", async () => {
        const settings = await repo.getSiteSettings();
        expect(settings).toBeDefined();
        expect(typeof settings.siteName).toBe("string");
        expect(settings.siteName.length).toBeGreaterThan(0);
      });

      it("getSiteSettings siteDescription is string or undefined", async () => {
        const settings = await repo.getSiteSettings();
        expect(
          settings.siteDescription === undefined ||
            typeof settings.siteDescription === "string",
        ).toBe(true);
      });

      describe("span emission", () => {
        it("getSiteSettings emits site-settings.getSiteSettings span with op=repository", async () => {
          if (!getTracer) return;
          const tracer = getTracer();
          tracer.reset();
          await repo.getSiteSettings();
          const span = tracer.findSpan("site-settings.getSiteSettings");
          expect(span).toBeDefined();
          expect(span!.op).toBe("repository");
        });
      });
    },
  );
