import { Schema, model } from 'mongoose'

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

const countrySchema = new Schema<ICountry>(
  {
    name: { type: String, trim: true, unique: true },
    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 },
)

countrySchema.plugin(toJSON)
countrySchema.plugin(paginate)

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

const Country = model<ICountry, ICountryModel>('Country', countrySchema)
export default Country
