// AS4 Settlement Message Schemas
// JSON Schema definitions for DBIS settlement messages

export const DBIS_SI_202_SCHEMA = {
  type: 'object',
  required: ['MessageId', 'BusinessType', 'CreatedAt', 'FromMemberId', 'ToMemberId', 'Instr'],
  properties: {
    MessageId: {
      type: 'string',
      pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
    },
    BusinessType: {
      type: 'string',
      enum: ['DBIS.SI.202', 'DBIS.SI.202COV'],
    },
    CreatedAt: {
      type: 'string',
      format: 'date-time',
    },
    FromMemberId: {
      type: 'string',
      minLength: 1,
    },
    ToMemberId: {
      type: 'string',
      minLength: 1,
    },
    CorrelationId: {
      type: 'string',
    },
    ReplayNonce: {
      type: 'string',
    },
    SchemaVersion: {
      type: 'string',
      default: '1.0',
    },
    Instr: {
      type: 'object',
      required: ['InstrId', 'ValueDate', 'Currency', 'Amount', 'DebtorAccount', 'CreditorAccount'],
      properties: {
        InstrId: {
          type: 'string',
          minLength: 1,
        },
        EndToEndId: {
          type: 'string',
        },
        ValueDate: {
          type: 'string',
          format: 'date',
        },
        Currency: {
          type: 'string',
          pattern: '^[A-Z]{3}$',
        },
        Amount: {
          type: 'string',
          pattern: '^[0-9]+\\.[0-9]{2,8}$',
        },
        DebtorAccount: {
          type: 'string',
          pattern: '^MSA:[A-Z0-9]+:[A-Z]{3}$',
        },
        CreditorAccount: {
          type: 'string',
          pattern: '^MSA:[A-Z0-9]+:[A-Z]{3}$',
        },
        Charges: {
          type: 'string',
          enum: ['BEN', 'SHA', 'OUR'],
        },
        FeeScheduleRef: {
          type: 'string',
        },
        PurposeCode: {
          type: 'string',
        },
        Narrative: {
          type: 'string',
        },
        CompliancePackageRef: {
          type: 'string',
        },
      },
    },
  },
};

export const DBIS_AD_910_SCHEMA = {
  type: 'object',
  required: ['MessageId', 'BusinessType', 'CreatedAt', 'FromMemberId', 'ToMemberId', 'Advice'],
  properties: {
    MessageId: {
      type: 'string',
    },
    BusinessType: {
      type: 'string',
      enum: ['DBIS.AD.910'],
    },
    CreatedAt: {
      type: 'string',
      format: 'date-time',
    },
    FromMemberId: {
      type: 'string',
      default: 'DBIS',
    },
    ToMemberId: {
      type: 'string',
    },
    CorrelationId: {
      type: 'string',
    },
    Advice: {
      type: 'object',
      required: ['AdviceId', 'OriginalInstrId', 'PostedAt', 'PostingId', 'CreditedAccount', 'Currency', 'Amount', 'FinalityStatus'],
      properties: {
        AdviceId: {
          type: 'string',
        },
        OriginalInstrId: {
          type: 'string',
        },
        PostedAt: {
          type: 'string',
          format: 'date-time',
        },
        PostingId: {
          type: 'string',
        },
        CreditedAccount: {
          type: 'string',
        },
        Currency: {
          type: 'string',
        },
        Amount: {
          type: 'string',
        },
        BalanceAfter: {
          type: 'string',
        },
        FinalityStatus: {
          type: 'string',
          enum: ['FINAL', 'PROVISIONAL'],
        },
      },
    },
  },
};

export const DBIS_AD_900_SCHEMA = {
  type: 'object',
  required: ['MessageId', 'BusinessType', 'CreatedAt', 'FromMemberId', 'ToMemberId', 'Advice'],
  properties: {
    MessageId: {
      type: 'string',
    },
    BusinessType: {
      type: 'string',
      enum: ['DBIS.AD.900'],
    },
    CreatedAt: {
      type: 'string',
      format: 'date-time',
    },
    FromMemberId: {
      type: 'string',
      default: 'DBIS',
    },
    ToMemberId: {
      type: 'string',
    },
    CorrelationId: {
      type: 'string',
    },
    Advice: {
      type: 'object',
      required: ['AdviceId', 'OriginalInstrId', 'PostedAt', 'PostingId', 'DebitedAccount', 'Currency', 'Amount', 'FinalityStatus'],
      properties: {
        AdviceId: {
          type: 'string',
        },
        OriginalInstrId: {
          type: 'string',
        },
        PostedAt: {
          type: 'string',
          format: 'date-time',
        },
        PostingId: {
          type: 'string',
        },
        DebitedAccount: {
          type: 'string',
        },
        Currency: {
          type: 'string',
        },
        Amount: {
          type: 'string',
        },
        BalanceAfter: {
          type: 'string',
        },
        FinalityStatus: {
          type: 'string',
          enum: ['FINAL', 'PROVISIONAL'],
        },
      },
    },
  },
};

export const MESSAGE_SCHEMAS: Record<string, any> = {
  'DBIS.SI.202': DBIS_SI_202_SCHEMA,
  'DBIS.SI.202COV': DBIS_SI_202_SCHEMA,
  'DBIS.AD.910': DBIS_AD_910_SCHEMA,
  'DBIS.AD.900': DBIS_AD_900_SCHEMA,
};
