import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.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 {
  final String? role;
  final String? agentTypeId;
  final String mode;

  const SocialLoginButtons({
    super.key,
    this.role,
    this.agentTypeId,
    this.mode = 'signup',
  });

  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,
        role: role,
        agentTypeId: agentTypeId,
        mode: mode,
      );
    }
  }

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

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // Google
        _SocialButton(
          onTap: isLoading
              ? null
              : () => ref.read(authProvider.notifier).signInWithGoogle(
                    role: role,
                    agentTypeId: agentTypeId,
                    mode: mode,
                  ),
          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,
          ),
        ),
      ],
    );
  }
}

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),
        ),
      ),
    );
  }
}
