import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { NoFilterProvidedError } from "../lib/errors.generated";
import { expectErr, expectOk } from "../testing/commandTestUtils";
import { baseStorageLocation, baseInactiveLocation } from "../testing/fixtures";
import { run } from "./listStorageLocations";

describe("listStorageLocations", () => {
  it("lists storage locations by site", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseStorageLocation, baseInactiveLocation]);

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

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

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

    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("returns error when no filter is provided", async () => {
    const { db } = createMockDb<DB>();

    const result = await run(db, {});
    expectErr(result as never, NoFilterProvidedError);
  });
});
