import mongoose, { Model } from 'mongoose';

import { IAvailabilitySummary, summarySchemeDesc } from './ReceivableAvailability.model';
import { MODEL_NAMES } from './_models';

const multipliersFields = ['insuredAvailability', 'uninsuredAvailability'];

export type SummaryMultipliers = Pick<IAvailabilitySummary, 'insuredAvailability' | 'uninsuredAvailability'>

export interface IReceivableAvailabilityManual {
  bbcDateId: mongoose.Types.ObjectId;
  summary: IAvailabilitySummary;
  multipliers: SummaryMultipliers;
}

type ReceivableAvailabilityManualSummaryModel = Model<IReceivableAvailabilityManual, {}, {}>;

export const multipliersSchemeDesc = Object.entries(summarySchemeDesc).reduce((acc, [key, value]) => {
  if (multipliersFields.includes(key)) {
    acc[key] = value;
  }
  return acc;
}, {});

export const ReceivableAvailabilityManualSummarySchema = new mongoose.Schema<IReceivableAvailabilityManual, ReceivableAvailabilityManualSummaryModel>(
  {
    bbcDateId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.BBCDates,
      required: true,
    },
    summary: summarySchemeDesc,
    multipliers: multipliersSchemeDesc,
  },
  { timestamps: true },
);

export const ReceivableAvailabilityManualSummary = mongoose.model<IReceivableAvailabilityManual, ReceivableAvailabilityManualSummaryModel>(MODEL_NAMES.receivableAvailabilityManualSummary, ReceivableAvailabilityManualSummarySchema);
