import { useState } from 'react';
import { UserCircle } from 'lucide-react';

export default function PwaProfileAvatar({ user, size = 'lg' }) {
  const [failed, setFailed] = useState(false);
  const imageUrl = !failed && (user?.avatar_url || user?.avatar || user?.profile_photo_url || user?.image_url);
  const initials = String(user?.name || 'کاربر')
    .trim()
    .split(/\s+/)
    .slice(0, 2)
    .map((part) => part[0])
    .join('');

  return (
    <span className={`pwa-profile-avatar ${size}`}>
      {imageUrl ? (
        <img src={imageUrl} alt={user?.name || 'پروفایل'} onError={() => setFailed(true)} />
      ) : size === 'header' ? (
        <UserCircle size={26} strokeWidth={1.8} />
      ) : (
        <b>{initials || 'ک'}</b>
      )}
    </span>
  );
}
