import { Schema, model } from 'mongoose';

import {
  ISupportDoc,
  ISupportModel,
} from '@/modules/support/support.interface';
import { paginate, toJSON } from '@/shared/utils/plugins/index';
import { SupportStatus } from '@/shared/constants/enum.constant';

const supportSchema = new Schema<ISupportDoc>(
  {
    userId: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    query: {
      type: String,
      required: true,
      trim: true,
    },
    reply: {
      type: String,
      trim: true,
      default: '',
    },
    status: {
      type: String,
      enum: SupportStatus,
      default: SupportStatus.PENDING,
    },
    repliedAt: {
      type: Date,
    },

    companyId: {
      type: Schema.Types.ObjectId,
      ref: 'Company',
    },

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

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

export const Support = model<ISupportDoc, ISupportModel>(
  'Support',
  supportSchema,
);
