import mongoose, { Document, Schema } from 'mongoose';

export interface IState extends Document {
  name: string;
  code?: string;
  country?: string;
  createdAt: Date;
  updatedAt: Date;
}

const StateSchema: Schema = new Schema(
  {
    name: {
      type: String,
      required: [true, 'State name is required'],
      trim: true,
      maxlength: [100, 'State name cannot exceed 100 characters']
    },
    code: {
      type: String,
      trim: true,
      maxlength: [10, 'Code cannot exceed 10 characters']
    },
    country: {
      type: String,
      default: 'India'
    }
  },
  { timestamps: true }
);

StateSchema.index({ name: 1 });

export default mongoose.models.states || mongoose.model<IState>('states', StateSchema);