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

export interface ISize extends Document {
  name: string;
  sortOrder: number;
  isActive: boolean;

  chest?: number;
  shoulder?: number;
  frontLength?: number;
  neck?: number;
  waist?: number;
  hip?: number;
  sleeveLength?: number;

  createdAt: Date;
  updatedAt: Date;
}

const SizeSchema: Schema = new Schema({
  name: {
  type: String,
  required: [true, 'Size name is required'],
  trim: true,
  maxlength: [20, 'Size name cannot exceed 20 characters'],
  unique: true // ✅ ADD THIS
},
  sortOrder: {
    type: Number,
    default: 0
  },
  isActive: {
    type: Boolean,
    default: true
  },

  // ✅ Measurements
  chest: {
    type: Number,
    min: 0
  },
  shoulder: {
    type: Number,
    min: 0
  },
  frontLength: {
    type: Number,
    min: 0
  },
  neck: {
    type: Number,
    min: 0
  },
  waist: {
    type: Number,
    min: 0
  },
  hip: {
    type: Number,
    min: 0
  },
  sleeveLength: {
    type: Number,
    min: 0
  }

}, {
  timestamps: true
});

SizeSchema.index({ sortOrder: 1 });
SizeSchema.index({ name: 1 }); // Already exists, now redundant if unique is on schema

export default mongoose.models.Size || mongoose.model<ISize>('Size', SizeSchema);