import { useQuery } from '@tanstack/react-query';
import { isEmpty } from 'lodash';
import { FG_PDF_STATUS, FG_SIGN_STATUS } from 'src/sections/bupot-21-26/constant';
import queryKey from 'src/sections/bupot-21-26/constant/queryKey';
import type { TBaseResponseAPI, TGetListDataTableBupotFinalResult } from '../types/types';
import finalTdkFinalApi from '../utils/api';

export const transformFgStatusToFgSignStatus = (fgStatus: any) => {
  // console.log('🚀 ~ transformFgStatusToFgSignStatus ~ fgStatus:', fgStatus);
  const splittedFgStatus = fgStatus?.split('-') || [];

  if (splittedFgStatus.includes('SIGN') > 0) {
    // failed
    return FG_SIGN_STATUS.FAILED;
  }

  if (splittedFgStatus.includes('SIGNING IN PROGRESS')) {
    return FG_SIGN_STATUS.IN_PROGRESS;
  }
  if (fgStatus === 'DUPLICATE') {
    return FG_SIGN_STATUS.DUPLICATE;
  }
  if (fgStatus === 'NOT_MATCH_STATUS') {
    return FG_SIGN_STATUS.NOT_MATCH_STATUS;
  }
  if (fgStatus === 'NOT_MATCH_NILAI') {
    return FG_SIGN_STATUS.NOT_MATCH_NILAI;
  }
  if (fgStatus === 'NOT_MATCH_IDBUPOT') {
    return FG_SIGN_STATUS.NOT_MATCH_IDBUPOT;
  }

  switch (splittedFgStatus[1]) {
    case 'document signed successfully':
    case 'Done':
      return FG_SIGN_STATUS.SIGNED;
    case 'SIGNING_IN_PROGRESS':
      return FG_SIGN_STATUS.IN_PROGRESS;
    case 'DUPLICATE':
      return FG_SIGN_STATUS.DUPLICATE;
    case 'NOT_MATCH_STATUS':
      return FG_SIGN_STATUS.NOT_MATCH_STATUS;
    case 'NOT_MATCH_IDBUPOT':
      return FG_SIGN_STATUS.NOT_MATCH_IDBUPOT;
    default:
      return null;
  }
};

export const getFgStatusPdf = (link: any, fgSignStatus: any) => {
  if (isEmpty(link) || [FG_SIGN_STATUS.IN_PROGRESS].includes(fgSignStatus))
    return FG_PDF_STATUS.TIDAK_TERSEDIA;

  if (!link.includes('https://coretaxdjp.pajak.go.id/')) return FG_PDF_STATUS.BELUM_TERBENTUK;

  return FG_PDF_STATUS.TERBENTUK;
};

export const transformSortModelToSortApiPayload = (transformedModel: any) => ({
  sortingMode: transformedModel.map((item: any) => item.field).join(','),
  sortingMethod: transformedModel.length > 0 ? transformedModel[0].sort : 'desc',
});

const normalisePropsGetBupotFinal = (params: TGetListDataTableBupotFinalResult[0]) => ({
  ...params,
  namaDokumenReferensi: params.dokumen_referensi[0].dokReferensi,
  nomorDokumenReferensi: params.dokumen_referensi[0].nomorDokumen,
  tanggalDokumenReferensi: params.dokumen_referensi[0].tanggal_Dokumen,
  fgSignStatus: transformFgStatusToFgSignStatus(params.fgStatus),
  fgPdf: getFgStatusPdf(params.link, transformFgStatusToFgSignStatus(params.fgStatus)),
  idDipotong: params.userId,
});

const normalisPropsParmas = (params: any) => {
  const sorting = !isEmpty(params.sort) ? transformSortModelToSortApiPayload(params.sort) : {};

  return {
    ...params,
    masaPajak: params.msPajak || null,
    tahunPajak: params.thnPajak || null,
    npwp: params.idDipotong || null,
    advanced: isEmpty(params.advanced) ? undefined : params.advanced,
    ...sorting,
    feature: 'tdkfinal',
  };
};
const useGetBupotFinal = ({ params, ...props }: any) => {
  const query = useQuery<TBaseResponseAPI<TGetListDataTableBupotFinalResult>>({
    queryKey: queryKey.bulanan.all(params),
    queryFn: async () => {
      const response = await finalTdkFinalApi.getList({ params: normalisPropsParmas(params) });

      return {
        ...response,
        data: response.data.map((data) => normalisePropsGetBupotFinal(data)),
      };
    },
    initialData: {
      data: [],
      total: 0,
    },
    refetchOnWindowFocus: false,
    ...props,
  });

  return query;
};

export default useGetBupotFinal;
