import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { basePaymentTerm, inactivePaymentTerm } from "../testing/fixtures";
import { run } from "./getSalesPaymentTerm";

describe("getSalesPaymentTerm", () => {
  it("returns payment term when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePaymentTerm]);

    const result = await run(mock.db, { id: basePaymentTerm.id });
    const { salesPaymentTerm } = expectOk<{ salesPaymentTerm: unknown }>(result as never);

    expect(salesPaymentTerm).not.toBeNull();
    expect(salesPaymentTerm).toMatchObject({
      id: basePaymentTerm.id,
      name: basePaymentTerm.name,
    });
  });

  it("returns inactive payment term when requested directly", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactivePaymentTerm]);

    const result = await run(mock.db, { id: inactivePaymentTerm.id });
    const { salesPaymentTerm } = expectOk<{ salesPaymentTerm: { status: string } }>(
      result as never,
    );

    expect(salesPaymentTerm.status).toBe("INACTIVE");
  });

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

    const result = await run(mock.db, { id: "nonexistent-id" });
    const { salesPaymentTerm } = expectOk<{ salesPaymentTerm: unknown }>(result as never);

    expect(salesPaymentTerm).toBeNull();
  });
});
