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

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

export function createTaxonomyNodeType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateTaxonomyNodeTypeParams<F>,
) {
  return db
    .type("TaxonomyNode", {
      code: db.string().unique().description("Globally unique, immutable node code"),
      name: db.string().description("Display name for the taxonomy node"),
      parentId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: "self" },
          backward: "children",
        })
        .description("Parent node ID (null for root nodes)"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const taxonomyNode = createTaxonomyNodeType({});
