'use client';

import FinanceDetail from '@/components/pages/finance/FinanceDetail';
import useSWR from 'swr';
import { useRouter, useSearchParams } from 'next/navigation';
import { FinanceApi } from '@/services/api/finance';
import { isResponseSuccess } from '@/lib/api-helper';

const FinanceDetailPage = () => {
  const router = useRouter();
  const financeId = useSearchParams().get('financeId');

  const { data: finance } = useSWR(financeId, () =>
    FinanceApi.getSingle(Number(financeId))
  );

  if (!financeId) {
    router.back();

    return (
      <div className='w-full flex flex-row justify-center items-center p-4'>
        <span className='loading loading-spinner loading-xl' />
      </div>
    );
  }

  // if (!finance || isResponseError(finance)) {
  //   router.replace('/404');
  //   return;
  // }

  return (
    <>
      {isResponseSuccess(finance) && <FinanceDetail finance={finance.data} />}
    </>
  );
};

export default FinanceDetailPage;
