import { ProductData as IProduct } from '../utils/interface/productInterface';
import { Schema, model, models } from 'mongoose';

const ProductSchema = new Schema<IProduct>({
  category: {
    type: String,
    required: [true, 'Please provide a category.'],
    maxlength: 100,
    trim: true,
  },
  name: {
    type: String,
    required: [true, 'Please provide a name.'],
    maxlength: 100,
    trim: true,
  },
  image: {
    type: String,
    required: [true, 'Please provide an image.'],
  },
  description: {
    type: String,
    required: [true, 'Please provide a description.'],
    trim: true,
  },
  place: {
    type: String,
    trim: true,
  },
  people: {
    type: String,
    trim: true,
  },
  process: {
    type: String,
    trim: true,
  },
  pairing: {
    type: String,
    trim: true,
  },
  available: {
    type: Boolean,
    default: true,
  },
  isFeatured: {
    type: Boolean,
    default: false,
  },
  price: {
    type: Number,
    required: [true, 'Please provide a price.'],
    validate(value: number) {
      if (value < 0) {
        throw new Error('Price must be a postive number');
      }
    },
  },
  customID: {
    type: String,
    required: [true, 'Please provide a custom id.'],
    unique: true,
  },
  stripeID: {
    type: String,
    required: [true, 'Please provide a stripe id.'],
    unique: true,
  },
});

const Product = models.Product || model<IProduct>('Product', ProductSchema);

module.exports = Product;
