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

const documentJobSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    name: {
      type: String,
      required: true,
      unique: true,
    },
    objectType: { type: String, required: false },
    object: {
      type: Schema.Types.ObjectId,
      refPath: 'objectType',
      required: true,
    },
    state: {
      type: { type: String, required: true, default: 'queued' },
      progress: { type: Number, required: false },
      message: { type: String, required: false },
    },
    documentTemplate: {
      type: Schema.Types.ObjectId,
      ref: 'documentTemplate',
      required: true,
    },
    documentPrinter: {
      type: Schema.Types.ObjectId,
      ref: 'documentPrinter',
      required: true,
    },
    content: {
      type: String,
      required: false,
    },
  },
  { timestamps: true }
);

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

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

export const documentJobModel = mongoose.model('documentJob', documentJobSchema);
