import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export enum EProspectFileType {
  FILE = 'FILE',
  AUDITOR_FILE = 'AUDITOR_FILE',
  AUDITOR_SHARED_FILE = 'AUDITOR_SHARED_FILE',
}

export interface IProspectFile {
  type: EProspectFileType;
  prospectId: mongoose.Types.ObjectId;
  group?: string;
  groupSpecial?: string;
  rowId?: string;
  auditorId?: mongoose.Types.ObjectId;
  isConfidential?: boolean;
  isDeclined?: boolean;
  fileId?: string;
  filename: string;
  uploadedAt: Date;
  uploadedBy?: string;
  sharepointId?: string;
  sharepointFolderId?: string;
  sharepointDriveId?: string;
  sharepointWebUrl?: string;
  sharepointPath?: string;
  isAccepted?: boolean;
  log?: string;
}

export interface IProspectFileDoc extends IProspectFile, Document {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IProspectFileLean extends IProspectFile {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IProspectFilePlain extends IProspectFile {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export type ProspectFileModel = Model<IProspectFileDoc>;

export const ProspectFileSchema = new mongoose.Schema<IProspectFileDoc>({
  isAccepted: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    required: true,
  },
  prospectId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: MODEL_NAMES.CRMProspects,
    required: true,
    index: true,
  },
  group: {
    type: String,
  },
  groupSpecial: {
    type: String,
  },
  rowId: {
    type: String,
  },
  auditorId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: MODEL_NAMES.CRMAuditors,
    index: true,
  },
  fileId: {
    type: String,
    required: true,
  },
  filename: {
    type: String,
    required: true,
  },
  isConfidential: {
    type: Boolean,
    default: false,
  },
  isDeclined: {
    type: Boolean,
    default: false,
  },
  uploadedAt: {
    type: Date,
    required: true,
  },
  uploadedBy: {
    type: String,
    required: false,
  },
  sharepointId: {
    type: String,
  },
  sharepointFolderId: {
    type: String,
  },
  sharepointDriveId: {
    type: String,
  },
  sharepointWebUrl: {
    type: String,
  },
  sharepointPath: {
    type: String,
  },
  log: {
    type: String,
  },
}, {
  timestamps: true,
  versionKey: false,
});

export const ProspectFile = mongoose.model<IProspectFileDoc>(MODEL_NAMES.CRMProspectFiles, ProspectFileSchema);
