import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { NoFilterProvidedError } from "../lib/errors.generated";
import { expectErr, expectOk } from "../testing/commandTestUtils";
import { baseCostLayer } from "../testing/fixtures";
import { run } from "./listCostLayers";

describe("listCostLayers", () => {
  it("lists cost layers by item", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseCostLayer]);

    const result = await run(mock.db, { itemId: "item-1" });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("item-1");
    expect(mock.selects[0].sql).toMatch(/"itemId"/);
  });

  it("lists cost layers by valuation policy", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseCostLayer]);

    const result = await run(mock.db, { valuationPolicyId: "policy-1" });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("policy-1");
    expect(mock.selects[0].sql).toMatch(/"valuationPolicyId"/);
  });

  it("lists cost layers by both item and valuation policy", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseCostLayer]);

    const result = await run(mock.db, { itemId: "item-1", valuationPolicyId: "policy-1" });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("item-1");
    expect(mock.selects[0].parameters).toContain("policy-1");
  });

  it("returns results in oldest-first order", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const olderLayer = {
      ...baseCostLayer,
      id: "cost-layer-old",
      createdAt: new Date("2024-01-01T00:00:00.000Z"),
    };
    const newerLayer = {
      ...baseCostLayer,
      id: "cost-layer-new",
      createdAt: new Date("2024-06-01T00:00:00.000Z"),
    };
    mock.enqueueResults([olderLayer, newerLayer]);

    const result = await run(mock.db, { itemId: "item-1" });
    const { items } = expectOk<{ items: { id: string; createdAt: Date }[]; hasNextPage: boolean }>(
      result as never,
    );

    expect(items).toHaveLength(2);
    expect(items[0].id).toBe("cost-layer-old");
    expect(items[1].id).toBe("cost-layer-new");
    expect(mock.selects[0].sql).toMatch(/order by .*"createdAt" asc/i);
  });

  it("returns empty list when no matches", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { itemId: "non-existent" });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(0);
  });

  it("returns error when no filter is provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {});
    expectErr(result as never, NoFilterProvidedError);
  });
});
