import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { basePendingReviewCostSummary, baseReviewedCostSummary } from "../testing/fixtures";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { StatusFilterRequiredError, CompanyScopeRequiredError } from "../lib/errors.generated";
import { run } from "./listManufacturingCostSummariesByStatus";

describe("listManufacturingCostSummariesByStatus", () => {
  it("lists pending-review cost summaries for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);

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

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

  it("lists reviewed cost summaries awaiting settlement", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseReviewedCostSummary]);

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

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

  it("filters summaries by site when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);

    const result = await run(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);
  });

  it("filters summaries by production order when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);

    const result = await run(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);
  });

  it("returns summary rows with production-order and status context", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePendingReviewCostSummary, baseReviewedCostSummary]);

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

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

  it("returns error when no status filter is provided", async () => {
    const { db } = createMockDb<DB>();

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

  it("returns error when company scope is missing", async () => {
    const { db } = createMockDb<DB>();

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