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

export interface GetDepartmentChildrenInput {
  departmentId: string;
  status?: string;
}

export async function run(db: ReadonlyDB<DB>, input: GetDepartmentChildrenInput) {
  let query = db
    .selectFrom("Department")
    .selectAll()
    .where("parentDepartmentId", "=", input.departmentId);

  if (input.status !== undefined) {
    query = query.where("status", "=", input.status);
  }

  const departments = await query.execute();
  return ok({ departments });
}
