import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get_it/get_it.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import 'core/config/environment.dart';
import 'core/permissions/permission_bootstrapper.dart';
import 'core/permissions/permission_manager.dart';
import 'core/routing/app_router.dart';
import 'core/theme/superport_shad_theme.dart';
import 'core/theme/theme_controller.dart';
import 'features/auth/application/auth_service.dart';
import 'features/masters/group/domain/repositories/group_repository.dart';
import 'features/masters/group_permission/domain/repositories/group_permission_repository.dart';
import 'injection_container.dart';

/// Superport 애플리케이션 진입점. 환경 초기화 후 앱 위젯을 실행한다.
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Environment.initialize();
  await initInjection(baseUrl: Environment.baseUrl);
  final authService = GetIt.I<AuthService>();
  try {
    await authService.refreshSession();
  } catch (error, stackTrace) {
    // 초기 자동 로그인 갱신이 실패하면 세션을 정리하고 로그인 화면으로 진입한다.
    debugPrint('세션 갱신 실패: $error');
    debugPrintStack(stackTrace: stackTrace);
    await authService.clearSession();
  }
  runApp(const SuperportApp());
}

/// 전체 앱을 구성하는 루트 위젯.
class SuperportApp extends StatefulWidget {
  const SuperportApp({super.key});

  @override
  State<SuperportApp> createState() => _SuperportAppState();
}

/// 테마/권한 스코프를 초기화하고 라우터를 구성한다.
class _SuperportAppState extends State<SuperportApp> {
  late final ThemeController _themeController;
  late final PermissionManager _permissionManager;

  @override
  void initState() {
    super.initState();
    _themeController = ThemeController();
    _permissionManager = PermissionManager();
    if (GetIt.I.isRegistered<PermissionManager>()) {
      GetIt.I.unregister<PermissionManager>();
    }
    GetIt.I.registerSingleton<PermissionManager>(_permissionManager);
    unawaited(_restorePermissions());
  }

  @override
  void dispose() {
    if (GetIt.I.isRegistered<PermissionManager>()) {
      GetIt.I.unregister<PermissionManager>();
    }
    _themeController.dispose();
    _permissionManager.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PermissionScope(
      manager: _permissionManager,
      child: ThemeControllerScope(
        controller: _themeController,
        child: AnimatedBuilder(
          animation: _themeController,
          builder: (context, _) {
            return ShadApp.router(
              title: 'Superport v2',
              routerConfig: appRouter,
              debugShowCheckedModeBanner: false,
              supportedLocales: const [Locale('ko', 'KR'), Locale('en', 'US')],
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              theme: SuperportShadTheme.light(),
              darkTheme: SuperportShadTheme.dark(),
              themeMode: _themeController.mode,
            );
          },
        ),
      ),
    );
  }

  Future<void> _restorePermissions() async {
    final authService = GetIt.I<AuthService>();
    final session = authService.session;
    if (session == null) {
      return;
    }
    final bootstrapper = PermissionBootstrapper(
      manager: _permissionManager,
      groupRepository: GetIt.I<GroupRepository>(),
      groupPermissionRepository: GetIt.I<GroupPermissionRepository>(),
    );
    await bootstrapper.apply(session);
  }
}
