import { ok, err, type ReadonlyDB } from "@tailor-platform/erp-kit/core";
import type { DB } from "../generated/kysely-tailordb";
import { WorkCenterNotFoundError } from "../lib/errors.generated";

export interface GetWorkCenterInput {
  id: string;
}

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

  if (!workCenter) {
    return err(new WorkCenterNotFoundError(input.id));
  }

  return ok({ workCenter });
}
