import type { QueryContext } from "@tailor-platform/erp-kit/core";
import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseCurrencyUSD } from "../testing/fixtures";
import { getBaseCurrency } from "./getBaseCurrency.generated";

describe("getBaseCurrency", () => {
  const ctx: QueryContext = { actorId: "test-actor" };

  it("returns base currency when set", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseCurrencyUSD]);

    const result = await getBaseCurrency(mock.db, undefined, ctx);

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

  it("returns null when no base currency is set", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await getBaseCurrency(mock.db, undefined, ctx);

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