import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:hyper_local/config/global.dart';
import 'package:hyper_local/utils/widgets/custom_toast.dart';

import '../config/helper.dart';

class AuthGuard {
  /// Check if the user is authenticated via Apple Login
  static bool isAppleUser() {
    if (Global.userData != null) {
      final provider = Global.userData!.authProvider?.toLowerCase();
      if (provider == 'apple' || provider == 'apple.com' || provider == 'apple_login') {
        return true;
      }
    }

    try {
      final user = FirebaseAuth.instance.currentUser;
      if (user != null) {
        for (final info in user.providerData) {
          if (info.providerId.toLowerCase() == 'apple.com') {
            return true;
          }
        }
      }
    } catch (_) {}

    return false;
  }

  /// Ensure the user is logged in.
  static Future<bool> ensureLoggedIn(BuildContext context) async {
    if (Global.userData != null && Global.userData!.token.isNotEmpty) {
      return true;
    }

    // Show toast message with authGuard type (includes Sign In button)
    ToastManager.show(
      context: context,
      type: ToastType.authGuard,
      fontSize: 14,
      message: AppHelpers.authMessage,
      fromAuthGuard: true,
    );

    return false;
  }
}
