import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { activeSite, inactiveSite } from "../testing/fixtures";
import { run } from "./getSite";

describe("getSite", () => {
  it("returns site when found by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeSite]);

    const result = await run(mock.db, { id: activeSite.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.site).toEqual(activeSite);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(activeSite.id);
    expect(mock.selects[0].sql).toMatch(/"id"/);
  });

  it("returns null when site not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { id: "nonexistent-id" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.site).toBeNull();
    }
  });

  it("returns ACTIVE site", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeSite]);

    const result = await run(mock.db, { id: activeSite.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.site!.status).toBe("ACTIVE");
    }
  });

  it("returns INACTIVE site", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveSite]);

    const result = await run(mock.db, { id: inactiveSite.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.site!.status).toBe("INACTIVE");
    }
  });
});
