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

const builtins = {
  code: db.string().unique().description("Globally unique, immutable attribute code"),
  name: db.string().description("Attribute display name"),
};

type ReservedFields = keyof typeof builtins | CommonReservedFields;

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

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

export const productAttribute = createProductAttributeType({});
