import {
  type CommonReservedFields,
  type NoReservedFields,
  defaultGqlPermission,
  defaultPermission,
} from "@tailor-platform/erp-kit/core";
import { db, type TailorAnyDBField } from "@tailor-platform/sdk";
import { currencyLifecycle } from "./currency.lifecycle.generated";

const builtins = {
  code: db.string().unique().description("ISO 4217 currency code (e.g., USD)"),
  name: db.string().description("Currency display name (e.g., US Dollar)"),
  symbol: db.string().description("Currency symbol (e.g., $)"),
  decimalPlaces: db.int().description("Number of decimal places (0-4)"),
  isBaseCurrency: db.bool().description("Whether this is the base currency"),
  status: db.enum(currencyLifecycle.states).description("Lifecycle status"),
};

type ReservedFields = keyof typeof builtins | CommonReservedFields;

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

export function createCurrencyType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateCurrencyTypeParams<F>,
) {
  return db
    .type("Currency", {
      ...builtins,
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const currency = createCurrencyType({});
