import {EyeIcon, EyeSlashIcon} from '@heroicons/react/24/outline'
import clsx from 'clsx'
import {useMemo, useState} from 'react'
import {Tooltip} from 'web/components/widgets/tooltip'
import {useHiddenProfiles} from 'web/hooks/use-hidden-profiles'
import {api} from 'web/lib/api'
import {useT} from 'web/lib/locale'

export type HideProfileButtonProps = {
  hiddenUserId: string
  onHidden?: (userId: string) => void
  className?: string
  iconClassName?: string
  tooltip?: string
  ariaLabel?: string
  stopPropagation?: boolean
  eyeOff?: boolean
  onPointerDown?: () => void
}

export function HideProfileButton(props: HideProfileButtonProps) {
  const {
    hiddenUserId,
    onHidden,
    className,
    iconClassName,
    tooltip,
    ariaLabel,
    stopPropagation,
    eyeOff,
    onPointerDown,
  } = props

  const t = useT()
  const [submitting, setSubmitting] = useState(false)
  const {hiddenProfiles, refreshHiddenProfiles} = useHiddenProfiles()
  const [optimisticHidden, setOptimisticHidden] = useState<boolean | undefined>(undefined)
  const hidden = useMemo(() => {
    if (optimisticHidden !== undefined) return optimisticHidden
    return hiddenProfiles?.some((u) => u.id === hiddenUserId) ?? false
  }, [hiddenProfiles, hiddenUserId, optimisticHidden])

  const onClick = async (e: React.MouseEvent) => {
    e.preventDefault()
    if (stopPropagation) e.stopPropagation()
    if (submitting) return
    setSubmitting(true)

    // Optimistically update hidden state
    setOptimisticHidden(!hidden)

    try {
      if (hidden) {
        await api('unhide-profile', {hiddenUserId})
      } else {
        await api('hide-profile', {hiddenUserId})
      }
      refreshHiddenProfiles()
      onHidden?.(hiddenUserId)
    } catch (e) {
      console.error('Failed to toggle hide profile', e)
      // Revert optimistic update on failure
      setOptimisticHidden(hidden)
    } finally {
      setSubmitting(false)
    }
  }

  return (
    <Tooltip
      text={
        hidden
          ? t('profile_grid.unhide_profile', 'Show again in search results')
          : (tooltip ?? t('profile_grid.hide_profile', "Don't show again in search results"))
      }
      noTap
    >
      <button
        data-testid="hide-profile-button"
        className={clsx(
          'relative inline-flex items-center justify-center border bg-canvas-50 border-canvas-300 text-ink-300 hover:border-primary-400 hover:bg-primary-50 rounded-lg h-7 w-7',
          className,
        )}
        disabled={submitting}
        onClick={onClick}
        onPointerDown={onPointerDown}
        aria-label={
          ariaLabel ??
          (hidden
            ? t('profile_grid.unhide_profile', 'Unhide this profile')
            : t('profile_grid.hide_profile', 'Hide this profile'))
        }
      >
        {hidden || eyeOff ? (
          <EyeSlashIcon className={clsx('h-4 w-4 text-ink-500', iconClassName)} />
        ) : (
          <EyeIcon className={clsx('h-4 w-4 text-ink-500', iconClassName)} />
        )}
      </button>
    </Tooltip>
  )
}

export default HideProfileButton
