import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/menu/view/widgets/profile/profile_view.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';

class ProfileTab extends StatelessWidget {
  const ProfileTab({
    super.key,
  });
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<HomeCubit, HomeState>(
      builder: (context, state) {
        return _buildProfileContent(context);
      },
    );
  }

  Widget _buildProfileContent(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(
        vertical: 10,
      ),
      child: InkWell(
        onTap: () {
          Navigator.of(context)
              .push(
            MaterialPageRoute(
              builder: (context) => const ProfileView(),
            ),
          )
              .then((result) {
            context.read<HomeCubit>().fetchUserInfo();
          });
        },
        child: Stack(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                const SizedBox(height: 20),
                DefaultContainer(
                  child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            BodyMedium(
                              text: '${HomeCubit.user?.firstName ?? ''} ',
                              fontWeight: FontWeight.bold,
                            ),
                            BodyMedium(
                              text: HomeCubit.user?.lastName ?? '',
                              fontWeight: FontWeight.bold,
                            ),
                          ],
                        ),
                        const SizedBox(
                          height: 5,
                        ),
                        const BodySmall(text: "Syncrow Account"),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            Positioned(
              right: 20,
              top: 0,
              child: CircleAvatar(
                radius: 38,
                backgroundColor: Colors.white,
                child: CircleAvatar(
                  radius: 37,
                  backgroundColor: Colors.grey,
                  child: ClipOval(
                    child: HomeCubit.user?.profilePicture != null
                        ? Image.memory(
                            HomeCubit.user!.profilePicture!,
                            fit: BoxFit.cover,
                            width: 110,
                            height: 110,
                          )
                        : const Icon(Icons.person, size: 70), // Fallback if no image
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
