import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:real_estate_mobile/core/constants/app_colors.dart';
import 'package:real_estate_mobile/features/auth/presentation/providers/auth_provider.dart';
import 'package:real_estate_mobile/features/auth/presentation/screens/twitter_login_screen.dart';

class SocialLoginButtons extends ConsumerWidget {
  const SocialLoginButtons({super.key});

  void _showComingSoon(BuildContext context) {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Coming soon'),
        duration: Duration(seconds: 1),
        behavior: SnackBarBehavior.floating,
      ),
    );
  }

  Future<void> _handleTwitterLogin(BuildContext context, WidgetRef ref) async {
    final result = await Navigator.push<Map<String, dynamic>?>(
      context,
      MaterialPageRoute(builder: (_) => const TwitterLoginScreen()),
    );
    if (result != null) {
      ref.read(authProvider.notifier).completeTwitterLogin(result);
    }
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final authState = ref.watch(authProvider);
    final isLoading = authState.status == AuthStatus.loading;

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // Apple
        _SocialButton(
          onTap: isLoading ? null : () => _showComingSoon(context),
          child: const Icon(
            Icons.apple,
            size: 28,
            color: AppColors.primaryDark,
          ),
        ),
        const SizedBox(width: 32),
        // Google
        _SocialButton(
          onTap: isLoading
              ? null
              : () => ref.read(authProvider.notifier).signInWithGoogle(),
          child: SvgPicture.asset(
            'assets/icons/google.svg',
            width: 24,
            height: 24,
          ),
        ),
        const SizedBox(width: 32),
        // X (Twitter)
        _SocialButton(
          onTap: isLoading
              ? null
              : () => _handleTwitterLogin(context, ref),
          child: SvgPicture.asset(
            'assets/icons/x.svg',
            width: 24,
            height: 24,
          ),
        ),
        const SizedBox(width: 32),
        // Facebook
        _SocialButton(
          onTap: isLoading
              ? null
              : () => ref.read(authProvider.notifier).signInWithFacebook(),
          child: SvgPicture.asset(
            'assets/icons/facebook.svg',
            width: 24,
            height: 24,
          ),
        ),
      ],
    );
  }
}

class _SocialButton extends StatelessWidget {
  final VoidCallback? onTap;
  final Widget child;

  const _SocialButton({
    required this.onTap,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Opacity(
        opacity: onTap != null ? 1.0 : 0.5,
        child: SizedBox(
          width: 40,
          height: 40,
          child: Center(child: child),
        ),
      ),
    );
  }
}
