import { db, type TailorAnyDBField } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";

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)
        .description("Costing method: FIFO, AVCO, or STANDARD_COST"),
      standardCostRate: db
        .decimal({ optional: true })
        .description("Fixed cost rate, required when costingMethod is STANDARD_COST"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const valuationPolicy = createValuationPolicyType({});
