import { Schema, model } from 'mongoose';
import { toJSON, paginate } from '@/shared/utils/plugins';
import { PaymentTerms, Status } from '@/shared/constants/enum.constant';
import { IPaymentTermDoc, IPaymentTermModel } from './paymetTerms.interface';

const paymentTermSchema = new Schema<IPaymentTermDoc>(
  {
    name: {
      type: String,
      required: true,
      trim: true,
    },
    stage: {
      type: String,
      required: true,
    },
    triggerType: {
      type: String,
      required: true,
      enum: Object.values(PaymentTerms),
    },
    triggerValue: {
      type: Number,
      required: true,
    },
    documentTemplate: {
      type: Schema.Types.ObjectId,
      ref: 'Documents',
      required: true,
    },
    paymentPlans: [
      {
        type: Schema.Types.ObjectId,
        ref: 'PaymentPlan',
        required: true,
      },
    ],
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.ACTIVE,
    },
    sendEmail: {
      type: Boolean,
      default: false,
    },
    sendSMS: {
      type: Boolean,
      default: false,
    },
    createdBy: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    updatedBy: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    project: {
      type: Schema.Types.ObjectId,
      ref: 'Project',
      required: true,
    },
  },
  {
    timestamps: true,
  },
);

paymentTermSchema.plugin(toJSON);
paymentTermSchema.plugin(paginate);

export const PaymentTerm = model<IPaymentTermDoc, IPaymentTermModel>(
  'PaymentTerm',
  paymentTermSchema,
);