const mongoose = require('mongoose')
const mongoosePaginate = require('mongoose-paginate-v2')
const timestamp = require('mongoose-timestamp')
const advertisingSchema = new mongoose.Schema({
  title: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  category: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  description: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 1024,
    default: null
  },
  province: {
    type: Object,
    default: null
  },
  city: {
    type: Object,
    default: null
  },
  neighbourhood: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  address: {
    type: String,
    trim: true,
    minLength: 1,
    maxLength: 512,
    default: null
  },
  lat: {
    type: String,
    trim: true,
    minLength: 1,
    maxLength: 256,
    default: null
  },
  lng: {
    type: String,
    trim: true,
    minLength: 1,
    maxLength: 256,
    default: null
  },
  images: [{
    type: String,
    required: false,
    trim: true,
    default: null
  }],
  services: [{
    title: {
      type: String,
      required: true,
      trim: true
    },
    originalPrice: {
      type: String,
      required: true
    },
    discountPrice: {
      type: String,
      required: false
    },
    discountPercentage: {
      type: Number,
      required: false
    },
    status: {
      type: String,
      default: "UnderReview",
      required: false
    },
    image: {
      type: String,
      required: false,
      trim: true
    }
  }],
  features: [{
    title: {
      type: String,
      required: false,
      trim: true
    },
    value: {
      type: Boolean,
      required: true
    }
  }],
  contactInfo: {
    phone: {
      type: String,
      required: false,
      trim: true,
      default: null
    },
    mobile: {
      type: String,
      required: false,
      trim: true,
      default: null
    },
    telegramLink: {
      type: String,
      required: false,
      trim: true,
      default: null
    },
    whatsappNumber: {
      type: String,
      required: false,
      trim: true,
      default: null
    },
    instagramLink: {
      type: String,
      required: false,
      trim: true,
      default: null
    },
    saveInfoForNextAds: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  type: {
    type: String,
    enum: ['free', 'normal', 'special', 'highlight'],
    default: 'normal'
  },
  status: {
    type: String,
    enum: ['pre_payment', 'paid', 'accepted', 'rejected', 'expired'],
    default: 'pre_payment'
  },
  payment_status: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 255,
    default: null
  },
  creator_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  reject_reason: {
    type: String,
    required: false,
    trim: true,
    minLength: 1,
    maxLength: 1024,
    default: null
  },
  mostDiscountPercentage: {
    type: Number,
    required: false
  },
  showDiscount: {
    type: Boolean,
    required: false,
    default: false
  },
  likesCount: {
    type: Number,
    default: 0
  },
  commentsCount: {
    type: Number,
    default: 0
  },
  ratings: [{
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true
    },
    rating: {
      type: Number,
      required: true,
      min: 1,
      max: 5
    }
  }],
  averageRating: {
    type: Number,
    default: 0
  },
  ratingsCount: {
    type: Number,
    default: 0
  },
  viewCount: {
    type: Number,
    default: 0
  },
  acceptedAt: {
    type: Date,
    default: null
  }
})

advertisingSchema.plugin(timestamp)
advertisingSchema.plugin(mongoosePaginate)
const AdvertisingModel = mongoose.model('Advertising', advertisingSchema)
module.exports = AdvertisingModel
