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

const builtins = {
  sourceCurrencyId: db.uuid().description("Foreign key to source Currency"),
  targetCurrencyId: db.uuid().description("Foreign key to target Currency"),
  rate: db.float().description("Conversion rate (source x rate = target)"),
  effectiveDate: db.date().description("Date from which this rate applies"),
};

type ReservedFields = keyof typeof builtins | CommonReservedFields;

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

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

export const exchangeRate = createExchangeRateType({});
