import { Schema, model } from 'mongoose';
import {
  IProjectChargeDoc,
  IProjectChargeModel,
} from '@/modules/projectCharge/projectCharge.interface.js';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import { ChargeType, Status } from '@/shared/constants/enum.constant';

const projectChargeSchema = new Schema<IProjectChargeDoc>(
  {
    companyId: { type: Schema.Types.ObjectId, ref: 'Company' },
    name: { type: String, required: true },
    project: { type: Schema.Types.ObjectId, ref: 'Project' },
    amount: { type: Number, required: true },
    chargeType: {
      type: String,
      enum: Object.values(ChargeType),
      default: ChargeType.PERCENTAGE,
    },
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.ACTIVE,
    },

    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  {
    timestamps: true,
  },
);

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

export const ProjectCharge = model<IProjectChargeDoc, IProjectChargeModel>(
  'ProjectCharge',
  projectChargeSchema,
);
