import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseFifoPolicy, baseStandardCostPolicy } from "../testing/fixtures";
import { expectOk } from "../testing/commandTestUtils";
import { run } from "./getValuationPolicy";

describe("getValuationPolicy", () => {
  it("returns valuation policy when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseFifoPolicy);

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

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

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

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