import {
  CRMEntity,
  CRMEntityNames,
  IAccount,
  IEvoAddproductType,
  IEvoEquipment,
} from '../types/entities';

type TGetName = (entity: any, targetName: string) => string;

export type TEntityProps = Record<'value', keyof CRMEntity> & {
  name?: keyof CRMEntity;
  getName?: TGetName;
};

type ExcludeFromPropsMap =
  | 'evo_currencychange'
  | 'evo_sot_coefficient_type'
  | 'evo_statuscode'
  | 'systemuser';

const propsMap: Record<
  Exclude<CRMEntityNames, ExcludeFromPropsMap>,
  TEntityProps
> = {
  account: {
    getName: (account: IAccount, targetName) => {
      if (['selectDealerPerson'].includes(targetName)) {
        return `
        ${account.name} 
        ${account.evo_inn || '-'} / ${account.evo_kpp || '-'}`;
      }
      return `${account.name} ${account.evo_kpp || ''}`;
    },
    value: 'accountid',
  },
  lead: {
    name: 'fullname',
    value: 'leadid',
  },
  opportunity: {
    name: 'name',
    value: 'opportunityid',
  },
  quote: {
    name: 'evo_quotename',
    value: 'quoteid',
  },
  contact: {
    name: 'fullname',
    value: 'contactid',
  },
  transactioncurrency: {
    name: 'isocurrencycode',
    value: 'transactioncurrencyid',
  },
  evo_client_type: {
    name: 'evo_name',
    value: 'evo_client_typeid',
  },
  evo_client_risk: {
    name: 'evo_name',
    value: 'evo_client_riskid',
  },
  evo_baseproduct: {
    name: 'evo_name',
    value: 'evo_id',
  },
  evo_leasingobject_type: {
    name: 'evo_name',
    value: 'evo_leasingobject_typeid',
  },
  evo_brand: {
    name: 'evo_name',
    value: 'evo_brandid',
  },
  evo_model: {
    name: 'evo_name',
    value: 'evo_modelid',
  },
  evo_equipment: {
    getName: (equipment: IEvoEquipment) =>
      `${equipment.evo_name} (${equipment.evo_start_production_year})`,
    value: 'evo_equipmentid',
  },
  evo_reward_condition: {
    name: 'evo_name',
    value: 'evo_reward_conditionid',
  },
  evo_gps_brand: {
    name: 'evo_name',
    value: 'evo_gps_brandid',
  },
  evo_gps_model: {
    name: 'evo_name',
    value: 'evo_gps_modelid',
  },
  evo_region: {
    name: 'evo_name',
    value: 'evo_regionid',
  },
  evo_town: {
    name: 'evo_name',
    value: 'evo_townid',
  },
  connection: {
    name: 'evo_name',
    value: 'connectionid',
  },
  evo_connection_role: {
    name: 'evo_name',
    value: 'evo_connection_roleid',
  },
  evo_impairment_group: {
    name: 'evo_name',
    value: 'evo_impairment_groupid',
  },
  evo_coefficient: {
    name: 'evo_name',
    value: 'evo_coefficientid',
  },
  evo_addproduct_type: {
    getName: (productType: IEvoAddproductType, targetName) => {
      if (
        [
          'selectRegistration',
          'selectTechnicalCard',
          'selectTelematic',
          'selectTracker',
        ].includes(targetName) &&
        productType.evo_graph_price !== undefined
      ) {
        return `${productType.evo_name} (${productType.evo_graph_price} руб.)`;
      }
      return `${productType.evo_name}`;
    },
    value: 'evo_addproduct_typeid',
  },
  evo_planpayment: {
    name: 'evo_name',
    value: 'evo_planpaymentid',
  },
  evo_tarif: {
    name: 'evo_name',
    value: 'evo_tarifid',
  },
  evo_rate: {
    name: 'evo_name',
    value: 'evo_id',
  },
  evo_subsidy: {
    name: 'evo_name',
    value: 'evo_subsidyid',
  },
};

export default propsMap;
