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

export interface GetPeriodCloseInput {
  accountingPeriodId: string;
}

/**
 * Function: getPeriodClose
 *
 * Retrieves a single period close by accounting period id.
 */
export async function run(db: ReadonlyDB<DB>, input: GetPeriodCloseInput) {
  const result = await db
    .selectFrom("PeriodClose")
    .selectAll()
    .where("accountingPeriodId", "=", input.accountingPeriodId)
    .executeTakeFirst();

  return ok({ periodClose: result ?? null });
}
