import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { activeCompany, draftCompany, inactiveCompany } from "../testing/fixtures";
import { run } from "./getCompany";

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

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

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

  it("returns null when company 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.company).toBeNull();
    }
  });

  it("returns DRAFT company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([draftCompany]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.company).toEqual(draftCompany);
      expect(result.value.company!.status).toBe("DRAFT");
    }
  });

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

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

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

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

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

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