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

const documentTemplateSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    name: {
      type: String,
      required: true,
      unique: true,
    },
    objectType: { type: String, required: false },
    tags: [{ type: String }],
    active: {
      type: Boolean,
      required: true,
      default: true,
    },
    global: {
      type: Boolean,
      required: true,
      default: false,
    },
    parent: {
      type: Schema.Types.ObjectId,
      ref: 'documentTemplate',
      required: false,
    },
    documentSize: {
      type: Schema.Types.ObjectId,
      ref: 'documentSize',
      required: true,
    },
    documentPrinters: [
      {
        type: Schema.Types.ObjectId,
        ref: 'documentPrinter',
        required: false,
      },
    ],
    content: {
      type: String,
      required: false,
      default: '<Container></Container>',
    },
    testObject: {
      type: Schema.Types.Mixed,
      required: false,
    },
  },
  { timestamps: true }
);

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

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

export const documentTemplateModel = mongoose.model('documentTemplate', documentTemplateSchema);
