const mongoose = require('mongoose');
const slugify = require('slugify');

const variationSchema = new mongoose.Schema({
    color: {
        type: String,
        required: true
    },
    colorCode: {
        type: String,
        required: true
    },
    colorImage: {
        type: String,
        required: true
    },
    image: {
        type: String,
        required: true
    }
});

const productSchema = new mongoose.Schema({
    id: {
        type: String,
        required: true,
        unique: true
    },
    category: {
        type: String,
        required: [true, 'Please provide product category'],
        enum: ['fashion', 'men', 'women', 'unisex']
    },
    type: {
        type: String,
        required: true
    },
    name: {
        type: String,
        required: [true, 'Please provide product name'],
        trim: true,
        maxlength: [100, 'Name cannot be more than 100 characters']
    },
    gender: {
        type: String,
        required: true,
        enum: ['men', 'women', 'unisex']
    },
    new: {
        type: Boolean,
        default: false
    },
    sale: {
        type: Boolean,
        default: false
    },
    hot: {
        type: Boolean,
        default: false
    },
    bestSeller: {
        type: Boolean,
        default: false
    },
    rate: {
        type: Number,
        min: 0,
        max: 5,
        default: 0
    },
    price: {
        type: Number,
        required: [true, 'Please provide product price'],
        min: [0, 'Price cannot be negative']
    },
    originPrice: {
        type: Number,
        min: [0, 'Original price cannot be negative']
    },
    brand: {
        type: String,
        required: true
    },
    sold: {
        type: Number,
        default: 0,
        min: 0
    },
    quantity: {
        type: Number,
        required: [true, 'Please provide stock quantity'],
        min: [0, 'Stock cannot be negative'],
        default: 0
    },
    quantityPurchase: {
        type: Number,
        default: 1,
        min: 1
    },
    sizes: [{
        type: String,
        enum: ['S', 'M', 'L', 'XL', 'XXL'],
        required: true
    }],
    variation: [variationSchema],
    thumbImage: [{
        type: String,
        required: true
    }],
    images: [{
        type: String,
        required: true
    }],
    description: {
        type: String,
        required: [true, 'Please provide product description'],
        maxlength: [1000, 'Description cannot be more than 1000 characters']
    },
    action: {
        type: String,
        enum: ['quick shop', 'add to cart', 'buy now'],
        default: 'quick shop'
    },
    slug: {
        type: String,
        unique: true,
        lowercase: true
    },
    createdAt: {
        type: Date,
        default: Date.now
    }
});

// Create index for search functionality
productSchema.index({ name: 'text', description: 'text' });

// Create slug from name before saving
productSchema.pre('save', function (next) {
    if (!this.isModified('name')) {
        return next();
    }
    this.slug = slugify(this.name, { lower: true });
    next();
});

// Create slug from name before updating
productSchema.pre('findOneAndUpdate', function (next) {
    const update = this.getUpdate();
    if (update && update.name) {
        update.slug = slugify(update.name, { lower: true });
    }
    next();
});

module.exports = mongoose.model('Product', productSchema); 