import mongoose, { Document, Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export enum ESharepointSyncStatus {
  pending = 'pending',
  synced = 'synced',
  failed = 'failed',
}

export const SharepointFolderTypes = {
  main: 'main',
  confidential: 'confidential',
} as const;

export type TSharepointFolderType = typeof SharepointFolderTypes[keyof typeof SharepointFolderTypes];

export interface IProspectSharepoint {
  folderType: TSharepointFolderType;
  isActive: boolean;
  prospectId: mongoose.Types.ObjectId;
  folderId: string;
  folderName: string;
  webUrl: string;
  driveId: string;
  syncStatus: ESharepointSyncStatus;
  parentId: string;
}

export type TProspectSharepointModel = Model<IProspectSharepoint>;

export interface IProspectSharepointDoc extends IProspectSharepoint, Document {
}

export const ProspectSharepointSchema = new mongoose.Schema<IProspectSharepoint, TProspectSharepointModel>(
  {
    folderType: {
      type: String,
      enum: SharepointFolderTypes,
      required: true,
      index: true,
    },
    isActive: {
      type: Boolean,
      default: true,
    },
    prospectId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.CRMProspects,
      required: true,
      index: true,
    },
    folderId: {
      type: String,
      required: true,
    },
    folderName: {
      type: String,
      required: true,
    },
    webUrl: {
      type: String,
      required: true,
    },
    driveId: {
      type: String,
      required: true,
    },
    syncStatus: {
      type: String,
      enum: Object.values(ESharepointSyncStatus),
      required: true,
    },
    parentId: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
    versionKey: false,
  },
);

export const ProspectSharepoint = mongoose.model<IProspectSharepoint, TProspectSharepointModel>(MODEL_NAMES.CRMProspectSharepoint, ProspectSharepointSchema);
