import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } 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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(activeSite);

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

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

  it("returns null when site not found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

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

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

  it("returns ACTIVE site", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(activeSite);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(inactiveSite);

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

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