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

export interface IUserType extends Document {
  role: string;
  roleId: number; // 1 = admin, 2 = user
  createdAt: Date;
  updatedAt: Date;
}

const UserTypeSchema: Schema = new Schema(
  {
    role: {
      type: String,
      required: [true, 'Role name is required'],
      trim: true,
      lowercase: true,
      enum: {
        values: ['admin', 'user'],
        message: 'Role must be either admin or user'
      }
    },
    roleId: {
      type: Number,
      required: [true, 'Role ID is required'],
      enum: {
        values: [1, 2],
        message: 'Role ID must be 1 (admin) or 2 (user)'
      },
      unique: true
    }
  },
  {
    timestamps: true, // automatically adds createdAt & updatedAt
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
  }
);

// ✅ Indexes
UserTypeSchema.index({ role: 1 }, { unique: true });
UserTypeSchema.index({ roleId: 1 }, { unique: true });

// ✅ Export model
export default mongoose.models.UserType ||
  mongoose.model<IUserType>('UserType', UserTypeSchema);