const mongoose = require('mongoose')

function productImage(img) {
  if (img) return '/uploads/images/products/' + img
}

function productPDF(file) {
  if (file) return '/uploads/pdf/' + file
}

const ProductImageSchema = mongoose.Schema({
  image: {type: String, get: productImage},
  created_at: String
}, {
  toObject: {getters: true},
  toJSON: {getters: true}
})

const ProductPDFSchema = mongoose.Schema({
  file: {
    fa: {type: String, get: productPDF},
    en: {type: String, get: productPDF}
  },
  pdf_details: {
    fa: {
      name: String
    },
    en: {
      name: String
    }
  },
  created_at: String
}, {
  toObject: {getters: true},
  toJSON: {getters: true}
})

const ProductSchema = mongoose.Schema({
  cover: {type: String, get: productImage},
  thumb: {type: String, get: productImage},
  images: [ProductImageSchema],
  more_section_image1: {type: String, get: productImage},
  more_section_image2: {type: String, get: productImage},
  render_image1: {type: String, get: productImage},
  render_image2: {type: String, get: productImage},
  chart_image: {type: String, get: productImage},
  pdf_files: [ProductPDFSchema],
  category: String,
  more_section: {type: Boolean, default: true},
  download_section: {type: Boolean, default: true},
  favorite: {type: Boolean, default: false},
  product_details: {
    fa: {
      name: String,
      short_description: String,
      page_title: String,
      page_description: String,
      more_title1: String,
      more_description1: String,
      more_title2: String,
      more_description2: String,
      render_caption1: String,
      render_caption2: String,
      chart_title: String,
      chart_description: String
    },
    en: {
      name: String,
      short_description: String,
      page_title: String,
      page_description: String,
      more_title1: String,
      more_description1: String,
      more_title2: String,
      more_description2: String,
      render_caption1: String,
      render_caption2: String,
      chart_title: String,
      chart_description: String
    }
  },
  created_at: String,
  updated_at: String
}, {
  toObject: {getters: true},
  toJSON: {getters: true}
})


module.exports = mongoose.model('Product', ProductSchema)
