import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'core/theme/app_theme.dart';
import 'core/router/app_router.dart';
import 'core/network/api_client.dart';
import 'core/auth/auth_service.dart';
import 'core/i18n/app_localizations.dart';
import 'core/i18n/locale_provider.dart';

final apiClientProvider = Provider<ApiClient>((ref) {
  const useMock = true;
  final client = ApiClient(
    baseUrl: 'https://your-api-host.com/api',
    useMock: useMock,
  );
  final authService = ref.read(authServiceProvider);
  client.setToken(authService.token);
  return client;
});

final authServiceProvider = Provider<AuthService>((ref) => AuthService());

final _routerProvider = Provider<GoRouter>((ref) => createAppRouter());

class App extends ConsumerWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final router = ref.watch(_routerProvider);
    final locale = ref.watch(localeProvider);
    TDTheme.needMultiTheme();
    return MaterialApp.router(
      key: ValueKey(locale),
      title: 'TBOSS OA',
      theme: AppTheme.light,
      routerConfig: router,
      locale: locale,
      supportedLocales: const [
        Locale('zh', 'CN'),
        Locale('en'),
        Locale('zh', 'TW'),
      ],
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      debugShowCheckedModeBanner: false,
    );
  }
}
