import { useState, useEffect } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Button } from '@/components/ui/button';
import { UserPlus, UserCheck, Loader2 } from 'lucide-react';
import { followUser, unfollowUser, getProfile, type UserProfile } from '../services/profileService';
import { useToast } from '@/hooks/useToast';
import { useAuthStore } from '@/stores/auth';

/**
 * FE-COMP-015: Follow/Unfollow button component for user profiles
 */

interface FollowButtonProps {
  userId: string;
  initialFollowing?: boolean;
  initialFollowerCount?: number;
  onFollowChange?: (isFollowing: boolean) => void;
  className?: string;
  size?: 'sm' | 'md' | 'lg';
  variant?: 'default' | 'outline' | 'ghost';
}

export function FollowButton({
  userId,
  initialFollowing = false,
  initialFollowerCount = 0,
  onFollowChange,
  className,
  size = 'md',
  variant,
}: FollowButtonProps) {
  const { user } = useAuthStore();
  const { success: showSuccess, error: showError } = useToast();
  const queryClient = useQueryClient();
  const [following, setFollowing] = useState(initialFollowing);
  const [followerCount, setFollowerCount] = useState(initialFollowerCount);
  const [isUpdating, setIsUpdating] = useState(false);

  // Fetch profile to get current follow status
  const { data: profile } = useQuery<UserProfile>({
    queryKey: ['userProfile', userId],
    queryFn: () => getProfile(userId),
    enabled: !!userId && userId !== user?.id,
    staleTime: 30000, // 30 seconds
  });

  // Update following state from profile if available
  useEffect(() => {
    if (profile && (profile as any).is_following !== undefined) {
      setFollowing((profile as any).is_following);
    } else if (initialFollowing !== undefined) {
      setFollowing(initialFollowing);
    }
  }, [profile, initialFollowing]);

  // Update follower count from profile
  useEffect(() => {
    if (profile?.followers_count !== undefined) {
      setFollowerCount(profile.followers_count);
    } else if (initialFollowerCount !== undefined) {
      setFollowerCount(initialFollowerCount);
    }
  }, [profile?.followers_count, initialFollowerCount]);

  // Don't show follow button if viewing own profile
  if (user?.id === userId) {
    return null;
  }

  const handleClick = async () => {
    if (isUpdating || !user) return;

    setIsUpdating(true);
    const newFollowing = !following;

    try {
      if (newFollowing) {
        await followUser(userId);
        showSuccess('Vous suivez maintenant cet utilisateur');
      } else {
        await unfollowUser(userId);
        showSuccess('Vous ne suivez plus cet utilisateur');
      }
      setFollowing(newFollowing);
      setFollowerCount((prev) => (newFollowing ? prev + 1 : prev - 1));
      onFollowChange?.(newFollowing);
      // Invalidate profile queries to refresh data
      queryClient.invalidateQueries({ queryKey: ['userProfile', userId] });
      queryClient.invalidateQueries({ queryKey: ['userProfile'] });
    } catch (error: any) {
      const errorMessage =
        error.response?.data?.error?.message ||
        error.response?.data?.message ||
        error.message ||
        'Erreur lors du changement de suivi';
      showError(errorMessage);
    } finally {
      setIsUpdating(false);
    }
  };

  // Don't show follow button if viewing own profile or not logged in
  if (user?.id === userId || !user) {
    return null;
  }

  const buttonVariant = variant || (following ? 'outline' : 'default');

  return (
    <Button
      onClick={handleClick}
      disabled={isUpdating}
      variant={buttonVariant}
      size={size}
      className={className || 'min-w-[100px]'}
    >
      {isUpdating ? (
        <>
          <Loader2 className="h-4 w-4 animate-spin mr-2" />
          {following ? 'Désabonnement...' : 'Abonnement...'}
        </>
      ) : following ? (
        <>
          <UserCheck className="h-4 w-4 mr-2" />
          Abonné
        </>
      ) : (
        <>
          <UserPlus className="h-4 w-4 mr-2" />
          Suivre
        </>
      )}
    </Button>
  );
}
