import { describe, vi } from "vitest";
import { RecordingTracer } from "@repo/core-testing/instrumentation";
import { SiteSettingsRepository } from "@/infrastructure/repositories/site-settings.repository";
import { siteSettingsRepositoryContract } from "@/__contracts__/site-settings-repository.contract";
import { stubPayloadConfig } from "@repo/core-testing/payload/stub-config";

// ---------------------------------------------------------------------------
// In-memory Payload stub for site-settings (Global)
// ---------------------------------------------------------------------------

function buildSiteSettingsStub() {
  const globalData: Record<string, unknown> = {
    siteName: "Contract Site",
    siteDescription: "A site for contract testing",
  };

  return {
    findGlobal: vi.fn(async () => {
      return { ...globalData };
    }),
  };
}

vi.mock("payload", () => ({
  getPayload: vi.fn(),
}));

// ---------------------------------------------------------------------------
// Contract suite
// ---------------------------------------------------------------------------

describe("SiteSettingsRepository", () => {
  describe("contract", () => {
    const tracer = new RecordingTracer();
    siteSettingsRepositoryContract.run(
      async () => {
        const stub = buildSiteSettingsStub();
        const { getPayload } = await import("payload");
        (getPayload as ReturnType<typeof vi.fn>).mockResolvedValue(stub);
        return new SiteSettingsRepository(stubPayloadConfig, tracer);
      },
      { tracer: () => tracer },
    );
  });
});
