import {
  db,
  type TailorAnyDBField,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";

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

export function createProductCategoryType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateProductCategoryTypeParams<F>,
) {
  return db
    .type("ProductCategory", {
      code: db.string().unique().description("Globally unique, immutable category code"),
      name: db.string().description("Category display name"),
      parentId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: "self" },
          backward: "children",
        })
        .description("Parent category ID (null for root categories)"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const productCategory = createProductCategoryType({});
