import type { OrganizationQueries, ItemManagementQueries } from "../module";

export type Site = NonNullable<
  Awaited<ReturnType<OrganizationQueries["getSite"]>>["value"]["site"]
>;

export type Item = NonNullable<
  Awaited<ReturnType<ItemManagementQueries["getItem"]>>["value"]["item"]
>;

export const mockOrgQueries = (site: Site | null): Pick<OrganizationQueries, "getSite"> => ({
  getSite: () => Promise.resolve({ ok: true as const, value: { site } }),
});

// Item & Record<string, unknown>: requires a full Item row but permits off-model extras
// (isManufacturable, valuation fields) that the command reads defensively.
export const mockItemQueries = (
  items: (Item & Record<string, unknown>)[],
): Pick<ItemManagementQueries, "getItem"> => ({
  getItem: (_db, input) =>
    Promise.resolve({
      ok: true as const,
      value: { item: "id" in input ? (items.find((item) => item.id === input.id) ?? null) : null },
    }),
});
