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

describe("listProductionOrdersByStatus", () => {
  it("lists released production orders for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
    spies.select.mockReturnValueOnce([
      { productionOrderId: "production-order-2", status: "COLLECTING" },
    ]);

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

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

  it("lists in-progress production orders for a site", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInProgressProductionOrder]);
    spies.select.mockReturnValueOnce([]);

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

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

  it("filters production orders by produced item when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
    spies.select.mockReturnValueOnce([]);

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

    expect(items).toHaveLength(1);
    expect(items[0]).toMatchObject({ orderedItemId: "item-1" });
  });

  it("filters production orders by planned date range when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
    spies.select.mockReturnValueOnce([]);

    const result = await run(db, {
      companyId: "company-1",
      status: "RELEASED",
      plannedStartDateFrom: new Date("2024-01-01T00:00:00.000Z"),
      plannedStartDateTo: new Date("2024-12-31T23:59:59.000Z"),
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("returns summary fields with site and cost-summary status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
    spies.select.mockReturnValueOnce([
      { productionOrderId: "production-order-2", status: "COLLECTING" },
    ]);

    const result = await run(db, {
      companyId: "company-1",
      status: "RELEASED",
    });
    const { items } = expectOk<{
      items: {
        siteId: string;
        status: string;
        plannedQuantity: number;
        plannedStartDate: Date;
        costSummaryStatus: string | null;
      }[];
      hasNextPage: boolean;
    }>(result as never);

    expect(items[0].siteId).toBe("site-1");
    expect(items[0].status).toBe("RELEASED");
    expect(items[0].plannedQuantity).toBe(100);
    expect(items[0].plannedStartDate).toEqual(new Date("2024-03-01T00:00:00.000Z"));
    expect(items[0].costSummaryStatus).toBe("COLLECTING");
  });

  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: "RELEASED",
    });
    expectErr(result as never, CompanyScopeRequiredError);
  });
});
