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,
  TGetListDataTableDn,
  TGetListDataTableDnResult,
} from '../types/types';
import tahunanA1Api from '../utils/api';

export const transformFgStatusToFgSignStatus = (fgStatus: any) => {
  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 normalisePropsGetBulanan = (params: TGetListDataTableDn) => ({
  ...params,
  fgSignStatus: transformFgStatusToFgSignStatus(params.fgStatus),
  fgPdf: getFgStatusPdf(params.link, transformFgStatusToFgSignStatus(params.fgStatus)),
  revNo: params.revNo,
  masaPajak: params.masaPajakAwal,
  pasalPPh: params.pasalPPh,
  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,
  };
};
const useGetTahunan = ({ params, ...props }: any) => {
  const query = useQuery<TBaseResponseAPI<TGetListDataTableDnResult>>({
    queryKey: queryKey.tahunanA1.all(params),
    queryFn: async () => {
      const response = await tahunanA1Api.getList({ params: normalisPropsParmas(params) });

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

  return query;
};

export default useGetTahunan;
