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

const connectionSchema = new Schema(
  {
    interface: { type: String, required: true },
    protocol: { type: String, required: true },
    host: { type: String, required: true },
    port: { type: Number, required: false },
  },
  { _id: false }
);

const documentPrinterSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    name: {
      type: String,
      required: true,
      unique: true,
    },
    connection: { type: connectionSchema, required: true },
    currentDocumentSize: { type: Schema.Types.ObjectId, ref: 'documentSize', required: false },
    tags: [{ type: String }],
    online: { type: Boolean, required: true, default: false },
    active: { type: Boolean, required: true, default: true },
    state: {
      type: { type: String, required: true, default: 'offline' },
      message: { type: String, required: false },
      progress: { type: Number, required: false },
    },
    connectedAt: { type: Date, default: null },
    host: { type: Schema.Types.ObjectId, ref: 'host', required: true },
    vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: false },
    queue: [{ type: Schema.Types.ObjectId, ref: 'documentJob', required: false }],
  },
  { timestamps: true }
);

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

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

export const documentPrinterModel = mongoose.model('documentPrinter', documentPrinterSchema);
