import mongoose from 'mongoose';

// Define Product record schema
const ProductRecordSchema = new mongoose.Schema({
  SKU: {
    type: String,
    required: true,
  },
  ProductName: {
    type: String,
    required: true,
  },
  QuantitySold: {
    type: Number,
    required: true,
  },
  SalesAmount: {
    type: Number,
    required: true,
  },
  comments:{
    type: String,
  },
});

// Define main Sales schema
const SalesSchema = new mongoose.Schema({
  uniqueId: {
    type: String,
    required: true,
    unique: true,
  },
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    refPath: 'userType',
    required: true,
  },
  userType: {
    type: String,
    required: true,
    enum: ['SalesCoOrdinator', 'TerritoryManager'],
  },
  addedFor: {
    type: String,
    required: true,
    enum: ['PrincipalDistributor', 'RetailDistributor'],
  },
  addedForId: {
    type: mongoose.Schema.Types.ObjectId,
    refPath: 'addedFor',
    required: true,
  },
  tradename: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    required: true
  },
  products: [ProductRecordSchema],
}, { timestamps: true, versionKey: false });

export const Sales = mongoose.model('Sales', SalesSchema);
