import 'package:base_project/routes/route_names.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SplashViewModel {
  Future<void> checkNavigation(BuildContext context) async {
    print("Checking navigation...");

    try {
      final prefs = await SharedPreferences.getInstance();
      final isLoggedIn = prefs.getBool('isLoggedIn') ?? false;

      // Wait for splash animations to complete (3 seconds total)
      await Future.delayed(const Duration(seconds: 3));

      if (!context.mounted) return; // Check if widget is still mounted

      if (!isLoggedIn) {
        print("Navigating to login...");
        // Navigate to login with fade transition
        Navigator.pushReplacementNamed(
          context,
          RouteNames.loginView,
        );
      } else {
        print("Navigating to home...");
        // Navigate to home with fade transition
        Navigator.pushReplacementNamed(
          context,
          RouteNames.homeView,
        );
      }
    } catch (e) {
      print("Error in splash navigation: $e");
      // Fallback to login on error
      if (context.mounted) {
        Navigator.pushReplacementNamed(
          context,
          RouteNames.loginView,
        );
      }
    }
  }
}
