import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'core/app_router.dart';
import 'core/app_theme.dart';
import 'core/navigation_provider.dart';

/// 健康管家 App 根组件
class HealthApp extends ConsumerWidget {
  const HealthApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      title: '健康管家',
      debugShowCheckedModeBanner: false,
      theme: AppTheme.lightTheme,
      home: const _RootNavigator(),
    );
  }
}

/// 根导航——根据 Riverpod 路由状态切换页面
class _RootNavigator extends ConsumerWidget {
  const _RootNavigator();

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final stack = ref.watch(routeStackProvider);
    final current = stack.last;

    return PopScope(
      canPop: stack.length <= 1,
      onPopInvokedWithResult: (didPop, result) {
        if (!didPop) popRoute(ref);
      },
      child: buildPage(current),
    );
  }
}
