import {
  db,
  type TailorAnyDBField,
  type TailorAnyDBType,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { company as companyStub } from "../lib/_db_deps";

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

export function createCostCenterHierarchyType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateCostCenterHierarchyTypeParams<F>,
) {
  return db
    .type("CostCenterHierarchy", {
      name: db.string().description("Hierarchy name"),
      version: db.string().description("Hierarchy version"),
      companyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.companyType ?? companyStub },
          backward: "costCenterHierarchies",
        })
        .description("Foreign key to Company"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const costCenterHierarchy = createCostCenterHierarchyType({});
