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 { baseFifoPolicy, baseStandardCostPolicy } from "../testing/fixtures";
import { run } from "./getValuationPolicy";

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

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

    expect(valuationPolicy).not.toBeNull();
    expect(valuationPolicy).toMatchObject({ id: baseFifoPolicy.id });
  });

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

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

    expect(valuationPolicy).toBeNull();
  });

  it("includes costing method and standard cost rate", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseStandardCostPolicy]);

    const result = await run(mock.db, { id: baseStandardCostPolicy.id });
    const { valuationPolicy } = expectOk<{
      valuationPolicy: { costingMethod: string; standardCostRate: string };
    }>(result as never);

    expect(valuationPolicy.costingMethod).toBe("STANDARD_COST");
    expect(valuationPolicy.standardCostRate).toBe("10.5");
  });
});
