// models/ProductColorImage.ts
import mongoose, { Document, Schema } from 'mongoose';

export interface IProductColorImage extends Document {
  product: mongoose.Types.ObjectId;
  color: mongoose.Types.ObjectId;
  images: {
    url: string;
    altText?: string;
    sortOrder: number;
    isPrimary: boolean;
  }[];
  createdAt: Date;
  updatedAt: Date;
}

const ProductColorImageSchema: Schema = new Schema({
  product: {
    type: Schema.Types.ObjectId,
    ref: 'Product',
    required: [true, 'Product ID is required'],
    index: true
  },
  color: {
    type: Schema.Types.ObjectId,
    ref: 'Color',
    required: [true, 'Color ID is required'],
    index: true
  },
  images: [{
    url: {
      type: String,
      required: [true, 'Image URL is required']
    },
    altText: {
      type: String,
      maxlength: [200, 'Alt text cannot exceed 200 characters']
    },
    sortOrder: {
      type: Number,
      default: 0
    },
    isPrimary: {
      type: Boolean,
      default: false
    }
  }]
}, {
  timestamps: true
});

ProductColorImageSchema.index({ product: 1, color: 1 }, { unique: true });

export default mongoose.models.ProductColorImage || mongoose.model<IProductColorImage>('ProductColorImage', ProductColorImageSchema);