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

import { MODEL_NAMES } from './_models';

export enum ETableNames {
  HIGH_LEVEL = 'HIGH_LEVEL',
  HIGH_LEVEL_APPROVED = 'HIGH_LEVEL_APPROVED',
  COMPLIANCE_ITEMS = 'COMPLIANCE_ITEMS',
  TOP_5_SKUS = 'TOP_5_SKUS',
  LAST_5_DAYS_TRANSACTIONS = 'LAST_5_DAYS_TRANSACTIONS',
  INVENTORY_TURN = 'INVENTORY_TURN',
  TOP_5_CUSTOMERS_CONCENTRATION = 'TOP_5_CUSTOMERS_CONCENTRATION',
  AGED_OUT_CUSTOMERS = 'AGED_OUT_CUSTOMERS',
}

export type IBorrowerSummaryTableData = {
  [key in ETableNames]: {
    title: string;
    data: string[][];
  };
}

export enum EChartNames {
  LAST_6_MONTH = 'LAST_6_MONTH',
}

export type IBorrowerSummaryChartData = {
  [key in EChartNames]: {
    series: {
      type: string,
      xKey: string,
      yKey: string,
      yName: string,
    }[],
    data: {
      bbc: string,
      revolverCollateralTotal: number,
      revolverLoanBalance: number,
    }[],
  };
}

export interface IBorrowerSummary {
  borrowerId: mongoose.Types.ObjectId;
  lastCalculatedAt: Date;
  tableData: IBorrowerSummaryTableData;
  chartData: IBorrowerSummaryChartData;
}

export interface IBorrowerSummaryDoc extends IBorrowerSummary, Document {
}

type BorrowerSummaryModel = Model<IBorrowerSummary, {}, {}>;

export const BorrowerSummarySchema = new mongoose.Schema<IBorrowerSummary, BorrowerSummaryModel>(
  {
    borrowerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.borrowers,
      required: true,
    },
    lastCalculatedAt: {
      type: Date,
      required: true,
    },
    tableData: mongoose.Schema.Types.Mixed,
    chartData: mongoose.Schema.Types.Mixed,
  },
  { timestamps: true },
);

export const BorrowerSummary = mongoose.model<IBorrowerSummary, BorrowerSummaryModel>(MODEL_NAMES.borrowerSummaries, BorrowerSummarySchema);
