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

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

export function createProductAttributeValueType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateProductAttributeValueTypeParams<F>,
) {
  return db
    .type("ProductAttributeValue", {
      attributeId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: productAttribute },
          backward: "productAttributeValues",
        })
        .description("Foreign key to ProductAttribute"),
      label: db.string().description("Display label for this value option"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .indexes({
      fields: ["attributeId", "label"],
      unique: true,
      name: "product_attribute_value_unique_idx",
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const productAttributeValue = createProductAttributeValueType({});
