import {
  type CommonReservedFields,
  type NoReservedFields,
  defaultGqlPermission,
  defaultPermission,
} from "@tailor-platform/erp-kit/core";
import { db, type TailorAnyDBField } from "@tailor-platform/sdk";

const builtins = {
  categoryId: db
    .string()
    .unique()
    .description("Stable string identifier for the category (catalog primary key)"),
  displayName: db.string().description("User-facing label rendered in preference UIs"),
  optOutAllowed: db
    .bool()
    .description("When false, the category is transactional and bypasses the preference filter"),
};

type ReservedFields = keyof typeof builtins | CommonReservedFields;

export interface CreateNotificationCategoryTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: NoReservedFields<F, ReservedFields>;
}

export function createNotificationCategoryType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateNotificationCategoryTypeParams<F>,
) {
  return db
    .type("NotificationCategory", {
      ...builtins,
      ...params.fields,
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const notificationCategory = createNotificationCategoryType({});
