import 'package:finnow_app/main.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:watch_it/watch_it.dart';

import 'auth/model.dart';

/// The main Finnow application.
class FinnowApp extends StatelessWidget with WatchItMixin {
  const FinnowApp({super.key});

  @override
  Widget build(BuildContext context) {
    final router = getIt<GoRouter>();
    
    // We add a top-level listener for anytime the authentication model changes.
    // If it does, we need to refresh navigation.
    registerChangeNotifierHandler(handler:(context, AuthenticationModel newValue, cancel) {
      if (newValue.state.authenticated()) {
        router.replace('/profiles');
      } else {
        while (router.canPop()) {// Clear the navigation stack.
          router.pop();
        }
        router.pushReplacement('/login');
      }
    },);

    return MaterialApp.router(
        routerConfig: router,
        title: 'Finnow',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
          useMaterial3: true,
        ));
  }
}
