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

const subJobSchema = new mongoose.Schema({
  _reference: { type: String, default: () => generateId()() },
  printer: {
    type: Schema.Types.ObjectId,
    ref: 'printer',
    required: true,
  },
  job: {
    type: Schema.Types.ObjectId,
    ref: 'job',
    required: true,
  },
  moonrakerJobId: {
    type: String,
    required: false,
  },
  gcodeFile: {
    type: Schema.Types.ObjectId,
    ref: 'gcodeFile',
    required: true,
  },
  state: {
    type: { required: true, type: String },
    progress: { required: false, type: Number },
  },
  number: {
    type: Number,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
  updatedAt: {
    type: Date,
    default: Date.now,
  },
  startedAt: { required: false, type: Date, default: null },
  finishedAt: { required: false, type: Date, default: null },
});

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

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

export const subJobModel = mongoose.model('subJob', subJobSchema);
