/* eslint-disable @typescript-eslint/naming-convention */
import { gql } from '@apollo/client';
import initializeApollo from 'apollo/client';
import type * as CRMTypes from 'gql/crm.types';
import type { Quote } from './index';

const QUERY_GET_REWARD_RULES = gql`
  query GetRewardRules($conditionId: Uuid!) {
    evo_reward_condition(evo_reward_conditionid: $conditionId) {
      evo_calc_reward_rules
    }
  }
`;

type QuoteSumFields =
  | 'evo_agent_reward_summ'
  | 'evo_broker_reward_summ'
  | 'evo_dealer_reward_summ'
  | 'evo_double_agent_reward_summ'
  | 'evo_dealer_broker_reward_summ'
  | 'evo_fin_department_reward_summ';

type QuoteTotalFields =
  | 'evo_agent_reward_total'
  | 'evo_broker_reward_total'
  | 'evo_dealer_reward_total'
  | 'evo_double_agent_reward_total'
  | 'evo_dealer_broker_reward_total'
  | 'evo_fin_department_reward_total';

async function getRewardSum(
  conditionId: string | null | undefined,
  quote: Quote,
  quoteRewardSummField: keyof Pick<Quote, QuoteSumFields>,
  quoteRewardTotalField: keyof Pick<Quote, QuoteTotalFields>
) {
  if (!conditionId) return 0;

  const apolloClient = initializeApollo();
  const {
    data: { evo_reward_condition },
  } = await apolloClient.query<CRMTypes.GetRewardRulesQuery, CRMTypes.GetRewardRulesQueryVariables>(
    {
      query: QUERY_GET_REWARD_RULES,
      variables: {
        conditionId,
      },
    }
  );

  if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) {
    return quote[quoteRewardSummField];
  }

  return quote[quoteRewardTotalField];
}

export default async function getSums(quote: Quote) {
  const [
    dealerRewardSumm,
    dealerBrokerRewardSumm,
    indAgentRewardSumm,
    calcDoubleAgentRewardSumm,
    calcBrokerRewardSum,
    finDepartmentRewardSumm,
  ] = await Promise.all([
    getRewardSum(
      quote?.evo_dealer_reward_conditionid,
      quote,
      'evo_dealer_reward_summ',
      'evo_dealer_reward_total'
    ),
    getRewardSum(
      quote?.evo_dealer_broker_reward_conditionid,
      quote,
      'evo_dealer_broker_reward_summ',
      'evo_dealer_broker_reward_total'
    ),
    getRewardSum(
      quote?.evo_agent_reward_conditionid,
      quote,
      'evo_agent_reward_summ',
      'evo_agent_reward_total'
    ),
    getRewardSum(
      quote.evo_double_agent_reward_conditionid,
      quote,
      'evo_double_agent_reward_summ',
      'evo_double_agent_reward_total'
    ),
    getRewardSum(
      quote.evo_broker_reward_conditionid,
      quote,
      'evo_broker_reward_summ',
      'evo_broker_reward_total'
    ),
    getRewardSum(
      quote.evo_fin_department_reward_conditionid,
      quote,
      'evo_fin_department_reward_summ',
      'evo_fin_department_reward_total'
    ),
  ]);

  return {
    dealerRewardSumm: dealerRewardSumm ?? 0,
    dealerBrokerRewardSumm: dealerBrokerRewardSumm ?? 0,
    indAgentRewardSumm: indAgentRewardSumm ?? 0,
    calcDoubleAgentRewardSumm: calcDoubleAgentRewardSumm ?? 0,
    calcBrokerRewardSum: calcBrokerRewardSum ?? 0,
    finDepartmentRewardSumm: finDepartmentRewardSumm ?? 0,
  };
}
