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

describe("listWarehouses", () => {
  it("lists warehouses by site", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveWarehouse]);

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

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

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

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

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

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

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

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

  it("orders by name ascending", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveWarehouse, baseInactiveWarehouse]);

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

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

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

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

    expect(items[0]).not.toHaveProperty("storageLocations");
  });
});
