import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export enum OrganizationEmailsType {
  SENDER = 'SENDER',
  PORTFOLIO = 'PORTFOLIO',
  HIDDEN_RECIPIENT = 'HIDDEN_RECIPIENT',
  FINANCIAL = 'FINANCIAL',
}

export interface IOrganizationEmail {
  _id?: mongoose.Types.ObjectId;
  organizationId: mongoose.Types.ObjectId;
  type: OrganizationEmailsType;
  title: string;
  address: string;
}

export type IOrganizationEmailStringified = {
  [K in keyof IOrganizationEmail]: K extends '_id' | 'productId' ? string : IOrganizationEmail[K];
};

type OrganizationEmailModel = Model<IOrganizationEmail>;

export const OrganizationEmailModelSchema = new mongoose.Schema<IOrganizationEmail, OrganizationEmailModel>(
  {
    organizationId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.organizations,
    },
    type: {
      type: String,
      enum: Object.values(OrganizationEmailsType),
      required: true,
      trim: true,
    },
    title: {
      type: String,
      required: true,
      trim: true,
    },
    address: {
      type: String,
      required: true,
      trim: true,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
  },
);

export const OrganizationEmail = mongoose.model<IOrganizationEmail, OrganizationEmailModel>(MODEL_NAMES.organizationEmails, OrganizationEmailModelSchema);
