import { Schema, model } from 'mongoose'

import { ICity, ICityModel } from '@/modules/master/location/location.interfaces.js'
import { paginate, toJSON } from '@/shared/utils/plugins/index.js'

const citySchema = new Schema<ICity>(
  {
    name: { type: String, trim: true, unique: true },
    state: { type: Schema.Types.ObjectId, ref: 'State'},
    loc: {
      type: {
        type: String,
        enum: ['Point'],
        required: true,
      },
      coordinates: {
        type: [Number],
        required: true,
      },
    },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  { timestamps: true },
)

citySchema.plugin(toJSON)
citySchema.plugin(paginate)

// Add 2dsphere index on loc with uniqueness
// citySchema.index({ loc: '2dsphere' }, { unique: true })

const City = model<ICity, ICityModel>('City', citySchema)
export default City
