import 'package:flutter/material.dart';
import 'package:prosapp_web_app/providers/auth_provider.dart';
import 'package:prosapp_web_app/router/router.dart';
import 'package:prosapp_web_app/services/navigation_service.dart';
import 'package:provider/provider.dart';

class NavbarAvatar extends StatelessWidget {
  const NavbarAvatar({super.key});

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<AuthProvider>(context).user!;
    final hasPicture = user.picture != null && user.picture!.isNotEmpty;
    final encoded = Uri.encodeComponent(user.name.isEmpty ? 'U' : user.name);
    final fallbackUrl =
        'https://ui-avatars.com/api/?name=$encoded&background=42A4EF&color=fff&size=80&bold=true&rounded=true';
    final imageUrl = hasPicture ? user.picture! : fallbackUrl;

    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        onTap: () => NavigationService.replaceTo(Flurorouter.profileRoute),
        child: SizedBox(
          width: 36,
          height: 36,
          child: ClipOval(
            child: Image.network(
              imageUrl,
              fit: BoxFit.cover,
              errorBuilder: (_, __, ___) =>
                  Image.network(fallbackUrl, fit: BoxFit.cover),
            ),
          ),
        ),
      ),
    );
  }
}

