import mongoose from 'mongoose';

export const EXPENSE_CATEGORIES = [
  'RENT',
  'ELECTRICITY',
  'SALARY',
  'TRANSPORT',
  'MAINTENANCE',
  'MARKETING',
  'INTERNET',
  'PACKAGING',
  'BANK_CHARGES',
  'MISCELLANEOUS',
  'OTHER',
];

const expenseSchema = new mongoose.Schema(
  {
    expenseDate: { type: Date, required: true, index: true },
    category: { type: String, enum: EXPENSE_CATEGORIES, required: true, index: true },
    subcategory: { type: String, default: '', trim: true, maxlength: 80 },
    amount: { type: Number, required: true, min: 0.01 },
    paymentMethod: {
      type: String,
      enum: ['CASH', 'UPI', 'CARD', 'BANK_TRANSFER', 'CHEQUE'],
      default: 'CASH',
    },
    vendor: { type: String, default: '', trim: true, maxlength: 120 },
    referenceNumber: { type: String, default: '', trim: true, maxlength: 60 },
    description: { type: String, default: '', trim: true, maxlength: 300 },
    attachment: { type: String, default: '' },
    notes: { type: String, default: '', trim: true, maxlength: 500 },
    createdBy: {
      id: { type: mongoose.Schema.Types.ObjectId, ref: 'Admin' },
      name: { type: String, default: '' },
    },
  },
  { timestamps: true }
);

expenseSchema.index({ expenseDate: -1, category: 1 });

export default mongoose.model('Expense', expenseSchema);
