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

const filamentSchema = new mongoose.Schema({
  _reference: { type: String, default: () => generateId()() },
  name: { required: true, type: String },
  barcode: { required: false, type: String },
  url: { required: false, type: String },
  image: { required: false, type: Buffer },
  color: { required: true, type: String },
  vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
  type: { required: true, type: String },
  cost: { required: true, type: Number },
  costTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: true },
  costWithTax: { required: true, type: Number },
  diameter: { required: true, type: Number },
  density: { required: true, type: Number },
  createdAt: { required: true, type: Date },
  updatedAt: { required: true, type: Date },
  emptySpoolWeight: { required: true, type: Number },
});

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

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

export const filamentModel = mongoose.model('filament', filamentSchema);
