import { useAccount, useReadContract } from 'wagmi'
import { formatUnits } from 'viem'
import { abis, getContractAddress } from '@/lib/contracts'
import { useTokenList } from './useTokenList'

/**
 * 查询 USDC 余额
 */
export function useUSDCBalance() {
  const { address, chainId } = useAccount()
  const { bySymbol } = useTokenList()
  const usdcToken = bySymbol['USDC']
  const contractAddress = chainId ? getContractAddress('USDC', chainId) : undefined

  const { data: balance, isLoading, error, refetch } = useReadContract({
    address: contractAddress,
    abi: abis.USDY,
    functionName: 'balanceOf',
    args: address ? [address] : undefined,
    query: {
      enabled: !!address && !!chainId && !!contractAddress,
      refetchInterval: 10000,
    }
  })

  const usdcDecimals = usdcToken?.onChainDecimals ?? usdcToken?.decimals ?? 18
  const formattedBalance = balance ? formatUnits(balance as bigint, usdcDecimals) : '0'

  return {
    balance: balance as bigint | undefined,
    formattedBalance,
    isLoading,
    error,
    refetch,
  }
}

/**
 * 查询 YT LP Token 余额
 */
export function useYTLPBalance() {
  const { address, chainId } = useAccount()
  const { bySymbol } = useTokenList()
  const lpToken = bySymbol['YTLPToken'] ?? bySymbol['LP']
  const contractAddress = chainId ? getContractAddress('YTLPToken', chainId) : undefined

  const { data: balance, isLoading, error, refetch } = useReadContract({
    address: contractAddress,
    abi: abis.YTLPToken,
    functionName: 'balanceOf',
    args: address ? [address] : undefined,
    query: {
      enabled: !!address && !!chainId && !!contractAddress,
      refetchInterval: 10000,
    }
  })

  const lpDecimals = lpToken?.onChainDecimals ?? lpToken?.decimals ?? 18
  const formattedBalance = balance ? formatUnits(balance as bigint, lpDecimals) : '0'

  return {
    balance: balance as bigint | undefined,
    formattedBalance,
    isLoading,
    error,
    refetch,
  }
}

/**
 * 通用 ERC20 余额查询（按合约地址）
 */
export function useTokenBalance(contractAddress: string | undefined, decimals: number = 18) {
  const { address } = useAccount()

  const { data: balance, isLoading, error, refetch } = useReadContract({
    address: contractAddress as `0x${string}` | undefined,
    abi: abis.YTToken,
    functionName: 'balanceOf',
    args: address ? [address] : undefined,
    query: {
      enabled: !!address && !!contractAddress,
      refetchInterval: 10000,
    }
  })

  const formattedBalance = balance ? formatUnits(balance as bigint, decimals) : '0'

  return {
    balance: balance as bigint | undefined,
    formattedBalance,
    isLoading,
    error,
    refetch,
  }
}

/**
 * 查询 USDC 授权额度
 */
export function useUSDCAllowance(spenderAddress?: `0x${string}`) {
  const { address, chainId } = useAccount()
  const { bySymbol } = useTokenList()
  const usdcToken = bySymbol['USDC']
  const contractAddress = chainId ? getContractAddress('USDC', chainId) : undefined

  const { data: allowance, isLoading, error, refetch } = useReadContract({
    address: contractAddress,
    abi: abis.USDY,
    functionName: 'allowance',
    args: address && spenderAddress ? [address, spenderAddress] : undefined,
    query: {
      enabled: !!address && !!chainId && !!spenderAddress && !!contractAddress,
    }
  })

  const usdcDecimals = usdcToken?.onChainDecimals ?? usdcToken?.decimals ?? 18
  const formattedAllowance = allowance ? formatUnits(allowance as bigint, usdcDecimals) : '0'

  return {
    allowance: allowance as bigint | undefined,
    formattedAllowance,
    isLoading,
    error,
    refetch,
  }
}
