import type { DB } from "../generated/kysely-tailordb";
import { ok, type ReadonlyDB } from "@tailor-platform/erp-kit/module";

export interface GetWarehouseInput {
  id: string;
}

export async function run(db: ReadonlyDB<DB>, input: GetWarehouseInput) {
  const warehouse = await db
    .selectFrom("Warehouse")
    .selectAll()
    .where("id", "=", input.id)
    .executeTakeFirst();

  if (!warehouse) {
    return ok({ warehouse: null, storageLocations: [] });
  }

  const storageLocations = await db
    .selectFrom("StorageLocation")
    .selectAll()
    .where("warehouseId", "=", warehouse.id)
    .execute();

  return ok({ warehouse, storageLocations });
}
