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

const jobSchema = new mongoose.Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    state: {
      type: { required: true, type: String },
      progress: { type: Number, required: false },
    },
    printers: [{ type: Schema.Types.ObjectId, ref: 'printer', required: false }],
    createdAt: { required: true, type: Date },
    updatedAt: { required: true, type: Date },
    startedAt: { required: false, type: Date },
    finishedAt: { required: false, type: Date },
    gcodeFile: {
      type: Schema.Types.ObjectId,
      ref: 'gcodeFile',
      required: false,
    },
    quantity: {
      type: Number,
      required: true,
      default: 1,
      min: 1,
    },
    subJobs: [{ type: Schema.Types.ObjectId, ref: 'subJob', required: false }],
    notes: [{ type: Schema.Types.ObjectId, ref: 'note', required: false }],
  },
  { timestamps: true }
);

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

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

export const jobModel = mongoose.model('job', jobSchema);
