import mongoose from "mongoose";

const variantSchema = new mongoose.Schema({
  name: String,
  sku: String,
  barcode: String,
  price: Number,
  stock: Number,
  attributes: Map
});

const productSchema = new mongoose.Schema(
  {
    vendor: { type: mongoose.Schema.Types.ObjectId, ref: "Vendor", required: true },
    name: { type: String, required: true, text: true },
    slug: { type: String, required: true, unique: true },
    description: String,
    category: { type: String, index: true },
    images: [String],
    price: { type: Number, required: true },
    oldPrice: Number,
    stock: { type: Number, default: 0 },
    sku: { type: String, index: true },
    barcode: String,
    status: { type: String, enum: ["draft", "pending", "approved", "rejected"], default: "pending" },
    tags: [String],
    variants: [variantSchema],
    rating: { type: Number, default: 0 },
    aiEmbeddingId: String
  },
  { timestamps: true }
);

productSchema.index({ name: "text", description: "text", category: "text", tags: "text" });

export const Product = mongoose.model("Product", productSchema);
