import { Schema, model } from 'mongoose';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import { ISubCategory, ISubCategoryModel } from '../property.interfaces';

const subCategorySchema = new Schema<ISubCategory>(
  {
    name: { type: String, required: true, trim: true, lowercase: true },
    categoryId: {
      type: Schema.Types.ObjectId,
      ref: 'Category',
      required: true,
    },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  { timestamps: true },
);

subCategorySchema.index({ name: 1, categoryId: 1 }, { unique: true });

subCategorySchema.plugin(toJSON);
subCategorySchema.plugin(paginate);

const SubCategory = model<ISubCategory, ISubCategoryModel>(
  'SubCategory',
  subCategorySchema,
);
export default SubCategory;
