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

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

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

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

  it("returns DRAFT company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(draftCompany);

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

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

    const result = await run(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");
    }
  });
});
