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

describe("getRouting", () => {
  it("returns a routing with ordered operations when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveRouting], [baseRoutingOperation, baseRoutingOperation2]);

    const result = await run(mock.db, { id: baseActiveRouting.id, companyId: "company-1" });
    const { routing, operations } = expectOk<{
      routing: unknown;
      operations: unknown[];
    }>(result as never);

    expect(routing).toMatchObject({ id: baseActiveRouting.id });
    expect(operations).toHaveLength(2);
    expect(mock.selects).toHaveLength(2);
    expect(mock.selects[0].parameters).toContain(baseActiveRouting.id);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].sql).toMatch(/"id"/);
    expect(mock.selects[0].sql).toMatch(/"companyId"/);
    expect(mock.selects[1].parameters).toContain(baseActiveRouting.id);
    expect(mock.selects[1].sql).toMatch(/"routingId"/);
    expect(mock.selects[1].sql).toMatch(/order by .*"sequenceNumber" asc/i);
  });

  it("returns inactive routing revisions when requested", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInactiveRouting], []);

    const result = await run(mock.db, { id: baseInactiveRouting.id, companyId: "company-1" });
    const { routing } = expectOk<{
      routing: { status: string };
    }>(result as never);

    expect(routing.status).toBe("INACTIVE");
  });

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

    const result = await run(mock.db, { id: baseDraftRouting.id, companyId: "company-1" });
    const { routing } = expectOk<{
      routing: { companyId: string };
    }>(result as never);

    expect(routing.companyId).toBe("company-1");
  });

  it("returns error when the routing does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

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

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