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

describe("listManufacturingCostSummariesByStatus", () => {
  it("lists pending-review cost summaries for a company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePendingReviewCostSummary]);

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

    expect(items).toHaveLength(1);

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"ProductionOrder"."companyId"/);
    expect(mock.selects[0].sql).toMatch(/"ManufacturingCostSummary"."status"/);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].parameters).toContain("PENDING_VARIANCE_REVIEW");
    expect(mock.selects[0].sql).toMatch(/order by .*"ManufacturingCostSummary"."id" asc/i);
  });

  it("lists reviewed cost summaries awaiting settlement", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseReviewedCostSummary]);

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

    expect(items).toHaveLength(1);

    expect(mock.selects[0].parameters).toContain("VARIANCE_REVIEWED");
  });

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

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

    expect(items).toHaveLength(1);

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

  it("filters summaries by production order when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePendingReviewCostSummary]);

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

    expect(items).toHaveLength(1);

    expect(mock.selects[0].sql).toMatch(/"ManufacturingCostSummary"."productionOrderId"/);
    expect(mock.selects[0].parameters).toContain("production-order-2");
  });

  it("returns summary rows with production-order and status context", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePendingReviewCostSummary, baseReviewedCostSummary]);

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

    expect(items).toHaveLength(2);

    expect(mock.selects[0].parameters).toContain("PENDING_VARIANCE_REVIEW");
    expect(mock.selects[0].parameters).toContain("VARIANCE_REVIEWED");
  });

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

    const result = await run(mock.db, {
      companyId: "company-1",
      status: [],
    });
    expectErr(result as never, StatusFilterRequiredError);
  });

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

    const result = await run(mock.db, {
      companyId: "",
      status: "PENDING_VARIANCE_REVIEW",
    });
    expectErr(result as never, CompanyScopeRequiredError);
  });
});
