import mongoose from 'mongoose';
import Joi from 'joi';

import { MODEL_NAMES } from './_models';

export interface IBankIncomingPaymentNotificationRule {
  bankId: mongoose.Types.ObjectId;
  deletedAt?: Date;
}

export type IBankIncomingPaymentNotificationRuleDoc = mongoose.HydratedDocument<IBankIncomingPaymentNotificationRule>;

export interface IBankIncomingPaymentNotificationRuleLean extends IBankIncomingPaymentNotificationRule {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IBankIncomingPaymentNotificationRulePlain extends Omit<IBankIncomingPaymentNotificationRule, 'bankId'> {
  _id: string;
  bankAccountId: string;
  createdAt: Date;
  updatedAt: Date;
}

export const BankIncomingPaymentNotificationRuleValidationSchema = Joi.object({
  _id: Joi.string(),
  bankId: Joi.string(),
  active: Joi.boolean(),
});

export type BankIncomingPaymentNotificationRuleModel = mongoose.Model<IBankIncomingPaymentNotificationRule>;

export const BankIncomingPaymentNotificationRuleSchema = new mongoose.Schema<IBankIncomingPaymentNotificationRule, BankIncomingPaymentNotificationRuleModel>(
  {
    bankId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.banks,
      required: true,
    },
    deletedAt: {
      type: Date,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: true },
    versionKey: false,
  },
);

export const BankIncomingPaymentNotificationRule = mongoose.model<IBankIncomingPaymentNotificationRule, BankIncomingPaymentNotificationRuleModel>(MODEL_NAMES.bankIncomingPaymentNotificationRules, BankIncomingPaymentNotificationRuleSchema);
