const mongoose = require('mongoose')

const PieceRequestSchema = mongoose.Schema({
  // system structure
  _openedBy: { type: mongoose.ObjectId, ref: 'User' },
  _updatedBy: { type: mongoose.ObjectId, ref: 'User' },
  user_id: { type: mongoose.ObjectId, ref: 'User' },
  agent_id: { type: mongoose.ObjectId, ref: 'Representation' },

  // form info
  description: String,
  createDate: String,
  sendDate: String,
  trackingCode: String,
  items: [
    {
      // product info
      ItemReceptionTransDate: String,
      ItemName: String,
      SerialNo1: String,
      ItemID: String,
      ItemCode: String,
      // GStartDate: String,
      // GEndDate: String,
      gStatus: Boolean,

      // piece info
      pieceName: String,
      pieceCode: String,
      pieceQuantity: Number,
      pieceId: { type: mongoose.ObjectId, ref: 'Piece' }
    }
  ],

  // admin operations
  seen: { type: Boolean, default: false },
  status: { type: String, enum: ['temp', 'sent', 'ongoing', 'waiting', 'delivered'], default: 'temp' },
  statusHistory: [
    {
      status: String,
      date: Number
    }
  ],
  deliveryCode: String
})

module.exports = mongoose.model('PieceRequest', PieceRequestSchema)
