import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { CompanyScopeRequiredError, ParentItemRequiredError } from "../lib/errors.generated";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { baseActiveRouting, baseDraftRouting, baseInactiveRouting } from "../testing/fixtures";
import { run } from "./listRoutingsByItem";

describe("listRoutingsByItem", () => {
  it("lists routing revisions for a produced item", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting, baseDraftRouting]);

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

    expect(items).toHaveLength(2);

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"companyId"/);
    expect(mock.selects[0].sql).toMatch(/"parentItemId"/);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].parameters).toContain("item-1");
    expect(mock.selects[0].sql).toMatch(/order by .*"revisionNumber" asc/i);
  });

  it("filters routing revisions by site when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting]);

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

    expect(items).toHaveLength(1);

    expect(mock.selects[0].sql).toMatch(/"siteId"/);
    expect(mock.selects[0].parameters).toContain("site-1");
  });

  it("filters routing revisions by status when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting]);

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

    expect(items).toHaveLength(1);

    expect(mock.selects[0].sql).toMatch(/"status"/);
    expect(mock.selects[0].parameters).toContain("ACTIVE");
  });

  it("returns all statuses by default", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting, baseDraftRouting, baseInactiveRouting]);

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

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

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

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

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

  it("returns routing revisions for the requested company scope", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting]);

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

    expect(items).toHaveLength(1);
    expect(items[0].companyId).toBe("company-1");
  });

  it("returns error when the parent item reference is missing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {
      companyId: "company-1",
      parentItemId: "",
    });
    expectErr(result as never, ParentItemRequiredError);
  });

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

    const result = await run(mock.db, { companyId: "", parentItemId: "item-1" });
    expectErr(result as never, CompanyScopeRequiredError);
  });
});
