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

type CategoryOrderByField = "categoryId" | "displayName" | "createdAt";

export type ListNotificationCategoriesInput = PaginationInput<CategoryOrderByField>;

/**
 * Returns the seeded NotificationCategory catalog as a paginated result.
 */
export async function run(db: ReadonlyDB<DB>, input: ListNotificationCategoriesInput) {
  const limit = input.limit ?? DEFAULT_PAGE_SIZE;
  const offset = input.offset ?? 0;
  const orderBy = input.orderBy ?? "categoryId";
  const orderDirection = input.orderDirection ?? "asc";

  const rows = await db
    .selectFrom("NotificationCategory")
    .selectAll()
    .orderBy(orderBy, orderDirection)
    .limit(limit + 1)
    .offset(offset)
    .execute();

  return ok(buildPaginatedResult(rows, limit));
}
