import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:anyway/presentation/providers/onboarding_state_provider.dart';
import 'package:anyway/presentation/pages/login.dart';
import 'package:anyway/presentation/pages/onboarding.dart';
import 'package:anyway/presentation/pages/new_trip_preferences.dart';

// Example providers (replace these with your actual providers)
// final onboardingStateProvider = Provider<bool>((ref) => true); // Replace with actual onboarding state logic
final authStateProvider = FutureProvider<bool>((ref) async => true); // Replace with actual auth state logic
final tripsAvailableProvider = FutureProvider<bool>((ref) async => false); // Replace with actual trips availability logic

class StartPage extends ConsumerWidget {
  const StartPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {

      // the home page is dependent on the state of the providers:
      // - if the user is not onboarded, show the onboarding flow
      // - if the user is not logged in, show the login page
      // - if there are no trips available, show the trip creation page
      // - else: show the overview page that shows the last trip

      final onboardingState = ref.watch(onboardingStateProvider);
      final authState = ref.watch(authStateProvider);
      final tripsAvailable = ref.watch(tripsAvailableProvider);

      return onboardingState.when(
        data: (isOnboarded) {
          if (!isOnboarded) {
            return const OnboardingPage();
          }

          return authState.when(
            data: (isLoggedIn) {
              if (!isLoggedIn) {
                return const LoginPage();
              }

              return tripsAvailable.when(
                data: (hasTrips) {
                  if (!hasTrips) {
                    return const TripCreationPage();
                  }
                  return const OverviewPage();
                },
                loading: () => const Scaffold(
                  body: Center(child: CircularProgressIndicator()),
                ),
                error: (error, stack) => Scaffold(
                  body: Center(child: Text('Error: $error')),
                ),
              );
            },
            loading: () => const Scaffold(
              body: Center(child: CircularProgressIndicator()),
            ),
            error: (error, stack) => Scaffold(
              body: Center(child: Text('Error: $error')),
            ),
          );
        },
        loading: () => const Scaffold(
          body: Center(child: CircularProgressIndicator()),
        ),
        error: (error, stack) => Scaffold(
          body: Center(child: Text('Error: $error')
        ),
      ),
    );
  }
}




class TripCreationPage extends StatelessWidget {
  const TripCreationPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Create a Trip')),
      body: Center(
        child: ElevatedButton.icon(
          icon: const Icon(Icons.tune),
          label: const Text('Set Preferences'),
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (_) => const NewTripPreferencesPage()));
          },
        ),
      ),
    );
  }
}

class OverviewPage extends StatelessWidget {
  const OverviewPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text('Overview Page')),
    );
  }
}
