import { Schema, model } from 'mongoose';
import {
  IPaymentPlanDoc,
  IPaymentPlanModel,
} from '@/modules/paymentPlan/paymentPlan.interface.js';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import { PaymentType, Status } from '@/shared/constants/enum.constant';

const paymentPlanSchema = new Schema<IPaymentPlanDoc>(
  {
    companyId: { type: Schema.Types.ObjectId, ref: 'Company' },
    name: { type: String, required: true },
    project: { type: Schema.Types.ObjectId, ref: 'Project' },
    downPayment: { type: Number, required: true },
    downPaymentType: {
      type: String,
      enum: Object.values(PaymentType),
      default: PaymentType.PERCENTAGE,
    },
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.ACTIVE,
    },
    ratePerSqYd: { type: Number },
    installmentMonth: { type: Number },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  {
    timestamps: true,
  },
);

// add plugin that converts mongoose to json
paymentPlanSchema.plugin(toJSON);
paymentPlanSchema.plugin(paginate);

export const PaymentPlan = model<IPaymentPlanDoc, IPaymentPlanModel>(
  'PaymentPlan',
  paymentPlanSchema,
);
