import { useQuery } from 'react-query';

import { useAuth } from 'src/contexts/JWTContext';
import { getUserSegmentations } from 'src/services/segmentations';
import { dynamicFieldsResponse } from 'src/services/users';
import { CUSTOM_FIELDS } from 'src/types/banBajio';
import { getProfileDataValue, getUserProfileData } from 'src/utils/banBajio';

import { banBajioKeys } from './queries';

export const useDynamicFields = () => {
  const { instance, user } = useAuth();
  return useQuery(
    banBajioKeys.dynamicFields(instance!.id),
    async () => {
      const response = await dynamicFieldsResponse(instance!.id);
      const isFiba = (await getUserSegmentations(user!.id)).data.some(
        (g: any) => g.item.name === 'FINANCIERA BAJIO',
      );
      return { ...response, isFiba };
    },
    {
      staleTime: Infinity,
      select: response => {
        const data = Object.entries(response.data).map(([key, v]) => ({
          id: key,
          name: v.name,
        }));

        const profileData = getUserProfileData(data, user!);

        const directorPosition = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.DIRECTOR_POSITION,
        );
        const payroll = getProfileDataValue(profileData, CUSTOM_FIELDS.PAYROLL);
        const boss = getProfileDataValue(profileData, CUSTOM_FIELDS.BOSS);
        const bossPosition = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.BOSS_POSITION,
        );
        const directorName = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.DIRECTOR_NAME,
        );

        const porcentualPoints = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.PORCENTUAL_POINTS,
        );
        const bankAccount = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.BANK_ACCOUNT,
        );
        const salary = getProfileDataValue(profileData, CUSTOM_FIELDS.SALARY);

        const shortTermDiscount = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.SHORT_TERM_LOAN_DISCOUNT,
        );
        const carDiscount = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.CAR_LOAN_DISCOUNT,
        );
        const furnitureDiscount = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.FURNITURE_LOAN_DISCOUNT,
        );
        const mortgageDiscount = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.MORTGAGE_LOAN_DISCOUNT,
        );

        const netFortnight = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.NET_FORTNIGHT,
        );

        const rfc = getProfileDataValue(profileData, CUSTOM_FIELDS.RFC);

        const department = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.DEPARTMENT,
        );

        const jobPosition = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.JOB_POSITION,
        );

        const street = getProfileDataValue(profileData, CUSTOM_FIELDS.STREET);

        const streetNumber = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.STREET_NUMBER,
        );

        const cologne = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.NEIGHBORHOOD,
        );

        const state = getProfileDataValue(profileData, CUSTOM_FIELDS.STATE);
        const city = getProfileDataValue(profileData, CUSTOM_FIELDS.CITY);
        const municipality = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.MUNICIPALITY,
        );
        const postalCode = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.POSTAL_CODE,
        );

        const levelPosition = getProfileDataValue(
          profileData,
          CUSTOM_FIELDS.LEVEL_POSITION,
        );

        const isValid = [
          netFortnight,
          salary,
          shortTermDiscount,
          carDiscount,
          furnitureDiscount,
          mortgageDiscount,
          levelPosition,
          user!.hiringDate,
          user!.birthdate,
        ].every(value => value !== null);

        return {
          directorPosition,
          payroll,
          boss,
          bossPosition,
          directorName,
          porcentualPoints,
          bankAccount,
          salary: Number(salary),
          shortTermDiscount: Number(shortTermDiscount),
          carDiscount: Number(carDiscount),
          furnitureDiscount: Number(furnitureDiscount),
          mortgageDiscount: Number(mortgageDiscount),
          rfc,
          department,
          jobPosition,
          street,
          streetNumber,
          cologne,
          state,
          city,
          municipality,
          postalCode,
          netFortnight: Number(netFortnight),
          isFiba: response.isFiba,
          levelPosition,
          isValid,
        };
      },
    },
  );
};
