import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseDraftMovement, baseDoneMovement } from "../testing/fixtures";
import { expectOk } from "../testing/commandTestUtils";
import { run } from "./listStockMovements";

describe("listStockMovements", () => {
  it("lists movements by status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseDraftMovement]);

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

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(baseDraftMovement);
  });

  it("lists movements by movement type", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseDraftMovement]);

    const result = await run(db, { movementType: "GOODS_RECEIPT" });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(baseDraftMovement);
  });

  it("lists movements by warehouse", async () => {
    const { db, spies } = createMockDb<DB>();
    // 1. StorageLocation ids by warehouseId
    spies.select.mockReturnValueOnce([{ id: "location-1" }]);
    // 2. StockMoveLine.stockMovementId where sourceLocationId in locationIds
    spies.select.mockReturnValueOnce([{ stockMovementId: "movement-1" }]);
    // 3. StockMoveLine.stockMovementId where destinationLocationId in locationIds
    spies.select.mockReturnValueOnce([{ stockMovementId: "movement-1" }]);
    // 4. StockMovement select filtered by unique movement ids
    spies.select.mockReturnValueOnce([baseDraftMovement]);

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

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(baseDraftMovement);
  });

  it("lists movements by date range", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseDraftMovement]);

    const result = await run(db, {
      dateFrom: new Date("2024-01-01"),
      dateTo: new Date("2024-12-31"),
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(baseDraftMovement);
  });

  it("returns empty list when no matches", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

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

    expect(items).toHaveLength(0);
    expect(hasNextPage).toBe(false);
  });

  it("orders by creation date descending", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseDoneMovement, baseDraftMovement]);

    const result = await run(db, {});
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("does not include movement lines", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseDraftMovement]);

    const result = await run(db, {});
    const { items } = expectOk<{ items: Record<string, unknown>[]; hasNextPage: boolean }>(
      result as never,
    );

    expect(items).toHaveLength(1);
    expect(items[0]).not.toHaveProperty("stockMoveLines");
  });
});
