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

export interface GetStockMovementInput {
  id: string;
}

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

  if (!stockMovement) {
    return ok({ stockMovement: null, stockMoveLines: [] });
  }

  const stockMoveLines = await db
    .selectFrom("StockMoveLine")
    .selectAll()
    .where("stockMovementId", "=", stockMovement.id)
    .execute();

  return ok({ stockMovement, stockMoveLines });
}
