import mongoose from 'mongoose';
import { generateId } from '../../utils.js';
const { Schema } = mongoose;

const taxRecordSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    taxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: true },
    transactionType: {
      type: String,
      required: true,
      enum: ['purchaseOrder', 'salesOrder', 'other'],
    },
    transaction: { type: Schema.Types.ObjectId, refPath: 'transactionType', required: true },
    amount: { type: Number, required: true },
    taxAmount: { type: Number, required: true },
    transactionDate: { required: true, type: Date, default: Date.now },
  },
  { timestamps: true }
);

taxRecordSchema.virtual('id').get(function () {
  return this._id;
});

taxRecordSchema.set('toJSON', { virtuals: true });

export const taxRecordModel = mongoose.model('taxRecord', taxRecordSchema);
