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

const builtins = {
  name: db.string().unique().description("Category name (e.g., Weight, Volume, Length)"),
  description: db.string({ optional: true }).description("Optional description of the category"),
  referenceUnitId: db
    .uuid({ optional: true })
    .description("Foreign key to the reference Unit for conversions"),
  status: db.enum(uoMCategoryLifecycle.states).description("Lifecycle status"),
};

type ReservedFields = keyof typeof builtins | CommonReservedFields;

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

export function createUoMCategoryType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateUoMCategoryTypeParams<F>,
) {
  return db
    .type("UoMCategory", {
      ...builtins,
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const uomCategory = createUoMCategoryType({});
