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

export const COSTING_METHODS = ["FIFO", "AVCO", "STANDARD_COST"] as const;

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

export function createValuationPolicyType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateValuationPolicyTypeParams<F>,
) {
  return db
    .type("ValuationPolicy", {
      name: db.string().description("Policy display name"),
      costingMethod: db
        .enum([...COSTING_METHODS] as [string, ...string[]])
        .description("Costing method: FIFO, AVCO, or STANDARD_COST"),
      standardCostRate: db
        .float({ optional: true })
        .description("Fixed cost rate, required when costingMethod is STANDARD_COST"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const valuationPolicy = createValuationPolicyType({});
