import { Schema, model, Types } from 'mongoose';
import {
  IDocumentsDoc,
  IDocumentsModel,
} from '@/modules/documents/documents.interface.js';
import { paginate, toJSON } from '@/shared/utils/plugins/index.js';
import {
  PAPER_SIZE,
  SCOPE,
  STATUS,
} from '@/modules/documents/documents.constant';

const documentsSchema = new Schema<IDocumentsDoc>(
  {
    companyId: { type: Schema.Types.ObjectId, ref: 'Company' },
    name: { type: String },
    paperSize: {
      type: String,
      enum: Object.values(PAPER_SIZE),
      default: PAPER_SIZE.A4,
    },
    scope: {
      type: String,
      required: true,
      enum: Object.values(SCOPE),
      default: SCOPE.ALL_PROJECTS,
    },

    projectIds: [
      {
        type: Types.ObjectId,
        ref: 'Project',
      },
    ],

    content: {
      type: String,
      required: true,
    },

    status: {
      type: String,
      enum: Object.values(STATUS),
      default: STATUS.PUBLISHED,
    },
    showLogoInHeader: {
      type: Boolean,
      default: false,
    },
    startFromHalfPage: {
      type: Boolean,
      default: false,
    },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
  },
  {
    timestamps: true,
  },
);

// add plugin that converts mongoose to json
documentsSchema.plugin(toJSON);
documentsSchema.plugin(paginate);

export const Documents = model<IDocumentsDoc, IDocumentsModel>(
  'Documents',
  documentsSchema,
);
