const mongoose = require('mongoose');

// Define the Sales schema
const salesSchema = new mongoose.Schema({
  branchId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Branch', 
    required: true 
  },
  companyId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Company', 
    required: true 
  },
  salesDate: { 
    type: Date, 
    required: true 
  },
  totalAmount: { 
    type: Number, 
    required: true 
  },
  createdAt: { 
    type: Date, 
    default: Date.now 
  },
  updatedAt: { 
    type: Date, 
    default: Date.now 
  }
});

// Create a unique index to enforce uniqueness on salesDate for each branch
salesSchema.index({ branchId: 1, salesDate: 1 }, { unique: true });

module.exports = mongoose.model('DailySales', salesSchema);
