import { TSchema, Data } from '@evolution-sdk/evolution';
import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
import { OutputReferenceSchema } from '@3rd-eye-labs/cardano-offchain-common';
import { OracleIdxSchema } from '../price-oracle/types-new';

const CDPCreatorDatumSchema = TSchema.Struct({
  creatorInputOref: OutputReferenceSchema,
});

const CreateCDPContentSchema = TSchema.Struct({
  cdpOwner: TSchema.ByteArray,
  minted: TSchema.Integer,
  collateralAmt: TSchema.Integer,
  currentTime: TSchema.Integer,
  creatorInputIdx: TSchema.Integer,
  creatorOutputIdx: TSchema.Integer,
  cdpOutputIdx: TSchema.Integer,
  iassetRefInputIdx: TSchema.Integer,
  collateralAssetRefInputIdx: TSchema.Integer,
  interestOracleRefInputIdx: TSchema.Integer,
  priceOracleIdx: OracleIdxSchema,
});

const CDPCreatorRedeemerSchema = TSchema.Union(
  TSchema.Struct(
    {
      CreateCDP: CreateCDPContentSchema,
    },
    { flatInUnion: true },
  ),
  TSchema.Literal('CreateStableswapPool', { flatInUnion: true }),
  TSchema.Literal('UpgradeCreatorVersion', { flatInUnion: true }),
);
export type CDPCreatorRedeemer = typeof CDPCreatorRedeemerSchema.Type;

export function serialiseCDPCreatorDatum(
  r: typeof CDPCreatorDatumSchema.Type,
): string {
  return Data.withSchema(
    CDPCreatorDatumSchema,
    DEFAULT_SCHEMA_OPTIONS,
  ).toCBORHex(r);
}

export function serialiseCDPCreatorRedeemer(r: CDPCreatorRedeemer): string {
  return Data.withSchema(
    CDPCreatorRedeemerSchema,
    DEFAULT_SCHEMA_OPTIONS,
  ).toCBORHex(r);
}
