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

describe("listWorkCentersBySite", () => {
  it("lists work centers for a site", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveWorkCenter, baseDraftWorkCenter]);

    const result = await run(mock.db, {
      companyId: "company-1",
      siteId: "site-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(/"siteId"/);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].parameters).toContain("site-1");
    expect(mock.selects[0].sql).toMatch(/order by .*"code" asc/i);
  });

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

    const result = await run(mock.db, {
      companyId: "company-1",
      siteId: "site-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("filters work centers by work-center type and accepts name ordering", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([{ ...baseActiveWorkCenter, name: "Assembly Cell A" }]);

    const result = await run(mock.db, {
      companyId: "company-1",
      siteId: "site-1",
      workCenterType: "ASSEMBLY",
      orderBy: "name",
    });
    const { items } = expectOk<{ items: { code: string }[]; hasNextPage: boolean }>(
      result as never,
    );

    expect(items).toHaveLength(1);
    expect(items[0].code).toBe(baseActiveWorkCenter.code);

    expect(mock.selects[0].sql).toMatch(/"workCenterType"/);
    expect(mock.selects[0].parameters).toContain("ASSEMBLY");
    expect(mock.selects[0].sql).toMatch(/order by .*"name" asc/i);
  });

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

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

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

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

    const result = await run(mock.db, { companyId: "", siteId: "site-1" });
    expectErr(result as never, SiteScopeRequiredError);
  });

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

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