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

const purchaseOrderSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    totalAmount: { type: Number, required: true },
    totalAmountWithTax: { type: Number, required: true },
    totalTaxAmount: { type: Number, required: true },
    timestamp: { type: Date, default: Date.now },
    vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
    state: {
      type: { type: String, required: true, default: 'draft' },
    },
  },
  { timestamps: true }
);

// Add virtual id getter
purchaseOrderSchema.virtual('id').get(function () {
  return this._id;
});

// Configure JSON serialization to include virtuals
purchaseOrderSchema.set('toJSON', { virtuals: true });

// Create and export the model
export const purchaseOrderModel = mongoose.model('purchaseOrder', purchaseOrderSchema);
