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

const partSchema = new mongoose.Schema({
  part: { type: Schema.Types.ObjectId, ref: 'part', required: true },
  quantity: { type: Number, required: true },
});

const gcodeFileSchema = new mongoose.Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    name: { required: true, type: String },
    gcodeFileName: { required: false, type: String },
    size: { type: Number, required: false },
    filament: { type: Schema.Types.ObjectId, ref: 'filament', required: true },
    parts: [partSchema],
    file: { type: mongoose.SchemaTypes.ObjectId, ref: 'file', required: false },
    cost: { type: Number, required: false },
  },
  { timestamps: true }
);

gcodeFileSchema.index({ name: 'text', brand: 'text' });

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

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

export const gcodeFileModel = mongoose.model('gcodeFile', gcodeFileSchema);
