import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:superport/models/equipment_unified_model.dart';
import 'package:superport/screens/common/app_layout.dart';
import 'package:superport/screens/common/theme_shadcn.dart';
import 'package:superport/screens/company/company_form.dart';
import 'package:superport/screens/company/branch_form.dart';
import 'package:superport/screens/equipment/equipment_in_form.dart';
import 'package:superport/screens/equipment/equipment_out_form.dart';
 // MaintenanceFormScreen으로 사용
import 'package:superport/screens/user/user_form.dart';
import 'package:superport/screens/warehouse_location/warehouse_location_form.dart';
import 'package:superport/services/auth_service.dart';
import 'package:superport/utils/constants.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:superport/screens/login/login_screen.dart';
import 'package:superport/injection_container.dart' as di;
import 'package:superport/screens/inventory/stock_in_form.dart';
import 'package:superport/screens/inventory/stock_out_form.dart';
import 'package:superport/screens/maintenance/maintenance_form_dialog.dart';
import 'package:superport/screens/vendor/controllers/vendor_controller.dart';
import 'package:superport/screens/model/controllers/model_controller.dart';
import 'package:superport/screens/equipment/controllers/equipment_history_controller.dart';
import 'package:superport/screens/maintenance/controllers/maintenance_controller.dart';
import 'package:superport/screens/maintenance/controllers/maintenance_dashboard_controller.dart';
import 'package:superport/screens/rent/controllers/rent_controller.dart';
import 'package:superport/core/config/environment.dart';
import 'package:superport/core/navigation/app_navigator.dart';

void main() async {
  // Flutter 바인딩 초기화
  WidgetsFlutterBinding.ensureInitialized();
  
  try {
    // 환경 변수 로드 (.env)
    await Environment.initialize();

    // 의존성 주입 설정
    await di.init();
  } catch (e) {
    print('Failed to setup dependencies: $e');
    // 에러가 발생해도 앱은 실행되도록 함
  }
  
  runApp(const SuperportApp());
}

class SuperportApp extends StatelessWidget {
  const SuperportApp({super.key});

  @override
  Widget build(BuildContext context) {
    AuthService? authService;
    try {
      authService = GetIt.instance<AuthService>();
    } catch (e) {
      print('Failed to get AuthService: $e');
    }
    
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<VendorController>(
          create: (_) => GetIt.instance<VendorController>(),
        ),
        ChangeNotifierProvider<ModelController>(
          create: (_) => GetIt.instance<ModelController>(),
        ),
        ChangeNotifierProvider<EquipmentHistoryController>(
          create: (_) => GetIt.instance<EquipmentHistoryController>(),
        ),
        ChangeNotifierProvider<MaintenanceController>(
          create: (_) => GetIt.instance<MaintenanceController>(),
        ),
        ChangeNotifierProvider<MaintenanceDashboardController>(
          create: (_) => GetIt.instance<MaintenanceDashboardController>(),
        ),
        ChangeNotifierProvider<RentController>(
          create: (_) => GetIt.instance<RentController>(),
        ),
      ],
      child: ShadApp(
        title: 'supERPort',
        materialThemeBuilder: (context, theme) => ShadcnTheme.lightTheme,
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: const [Locale('ko', 'KR'), Locale('en', 'US')],
        locale: const Locale('ko', 'KR'),
        home: authService == null
            ? const LoginScreen() // AuthService가 없으면 바로 로그인 화면
            : FutureBuilder<bool>(
                future: authService.isLoggedIn(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const Scaffold(
                      body: Center(
                        child: CircularProgressIndicator(),
                      ),
                    );
                  }
                  
                  // 에러 처리 추가
                  if (snapshot.hasError) {
                    print('Auth check error: ${snapshot.error}');
                    // 에러가 발생해도 로그인 화면으로 이동
                    return const LoginScreen();
                  }
                  
                  if (snapshot.hasData && snapshot.data!) {
                    // 토큰이 유효하면 홈 화면으로
                    return AppLayout(initialRoute: Routes.home);
                  } else {
                    // 토큰이 없거나 유효하지 않으면 로그인 화면으로
                    return const LoginScreen();
                  }
                },
              ),
        onGenerateRoute: (settings) {
        // 로그인 라우트 처리
        if (settings.name == '/login') {
          return MaterialPageRoute(builder: (context) => const LoginScreen());
        }
        // 기본 AppLayout으로 라우팅할 경로 (홈, 목록 화면들)
        if (settings.name == Routes.home ||
            settings.name == Routes.vendor ||
            settings.name == Routes.equipment ||
            settings.name == Routes.equipmentInList ||
            settings.name == Routes.equipmentOutList ||
            settings.name == Routes.equipmentRentList ||
            settings.name == Routes.company ||
            settings.name == Routes.user ||
            settings.name == Routes.inventory ||
            settings.name == Routes.inventoryHistory ||
            settings.name == Routes.maintenance ||
            settings.name == Routes.maintenanceSchedule ||
            settings.name == Routes.maintenanceAlert ||
            settings.name == Routes.maintenanceHistory) {
          return MaterialPageRoute(
            builder:
                (context) => AppLayout(initialRoute: settings.name!),
          );
        }

        // 기존 라우팅 처리 (폼 화면들)
        switch (settings.name) {
          // 장비 입고 관련 라우트 (새로운 Lookup API 기반)
          case Routes.equipmentInAdd:
            return MaterialPageRoute(
              builder: (context) => const EquipmentInFormScreen(),
            );
          case Routes.equipmentInEdit:
            final args = settings.arguments;
            if (args is Map<String, dynamic>) {
              // 새로운 방식: Map으로 전달받은 경우
              return MaterialPageRoute(
                builder: (context) => EquipmentInFormScreen(
                  equipmentInId: args['equipmentId'] as int?,
                  preloadedData: args,
                ),
              );
            } else if (args is int) {
              // 이전 방식 호환: int만 전달받은 경우
              return MaterialPageRoute(
                builder: (context) => EquipmentInFormScreen(equipmentInId: args),
              );
            } else {
              // 기본값
              return MaterialPageRoute(
                builder: (context) => const EquipmentInFormScreen(),
              );
            }

          // 장비 출고 관련 라우트
          case Routes.equipmentOutAdd:
            // 선택된 장비 정보와 입고 ID가 전달되었는지 확인
            final args = settings.arguments;
            Equipment? equipment;
            int? equipmentInId;
            List<Map<String, dynamic>>? selectedEquipments;

            // 인자 처리
            if (args is Map<String, dynamic>) {
              // 다중 선택 장비 처리
              if (args.containsKey('selectedEquipments')) {
                selectedEquipments =
                    args['selectedEquipments'] as List<Map<String, dynamic>>;
                debugPrint('선택된 장비 목록: ${selectedEquipments.length}개');
              } else {
                // 단일 장비 선택 (기존 방식)
                equipment = args['equipment'] as Equipment?;
                equipmentInId = args['equipmentInId'] as int?;
                debugPrint('단일 장비 선택');
              }
            } else if (args is List<Map<String, dynamic>>) {
              // 직접 리스트가 전달된 경우
              selectedEquipments = args;
              debugPrint('직접 리스트로 전달된 장비 목록: ${selectedEquipments.length}개');
            } else if (args is Equipment) {
              equipment = args; // 기존 방식 대응 (하위 호환)
              debugPrint('단일 Equipment 객체 전달');
            } else {
              debugPrint('알 수 없는 인자 타입: ${args.runtimeType}');
            }

            return MaterialPageRoute(
              builder:
                  (context) => EquipmentOutFormScreen(
                    selectedEquipment: equipment,
                    selectedEquipmentInId: equipmentInId,
                    selectedEquipments: selectedEquipments,
                  ),
            );
          case Routes.equipmentOutEdit:
            final id = settings.arguments as int;
            return MaterialPageRoute(
              builder: (context) => EquipmentOutFormScreen(equipmentOutId: id),
            );

          // 회사 관련 라우트 (단순화된 폼 사용)
          case Routes.companyAdd:
            return MaterialPageRoute(
              builder: (context) => const CompanyFormScreen(),
            );
          case Routes.companyEdit:
            final args = settings.arguments;
            if (args is Map) {
              return MaterialPageRoute(
                builder: (context) => CompanyFormScreen(args: args),
              );
            } else if (args is int) {
              // 하위 호환: int만 넘어오는 경우
              return MaterialPageRoute(
                builder:
                    (context) => CompanyFormScreen(args: {'companyId': args}),
              );
            } else {
              return MaterialPageRoute(
                builder: (context) => CompanyFormScreen(),
              );
            }

          // 지점 추가 라우트
          case '/company/branch/add':
            return MaterialPageRoute(
              builder: (context) => const BranchFormScreen(),
            );

          // 지점 수정 라우트
          case '/company/branch/edit':
            final args = settings.arguments as Map<String, dynamic>;
            final branchId = args['branchId'] as int;
            final parentCompanyName = args['parentCompanyName'] as String?;
            return MaterialPageRoute(
              builder: (context) => BranchFormScreen(
                branchId: branchId,
                parentCompanyName: parentCompanyName,
              ),
            );

          // 사용자 관련 라우트
          case Routes.userAdd:
            return MaterialPageRoute(
              builder: (context) => const UserFormScreen(),
            );
          case Routes.userEdit:
            final id = settings.arguments as int;
            return MaterialPageRoute(
              builder: (context) => UserFormScreen(userId: id),
            );

          // License 시스템이 Maintenance로 대체됨

          // 입고지 관련 라우트
          case Routes.warehouseLocationAdd:
            return MaterialPageRoute(
              builder: (context) => const WarehouseLocationFormScreen(),
            );
          case Routes.warehouseLocationEdit:
            final args = settings.arguments;
            if (args is Map<String, dynamic>) {
              // 새로운 방식: Map으로 전달받은 경우
              return MaterialPageRoute(
                builder: (context) => WarehouseLocationFormScreen(
                  id: args['locationId'] as int?,
                  preloadedData: args,
                ),
              );
            } else if (args is int) {
              // 이전 방식 호환: int만 전달받은 경우
              return MaterialPageRoute(
                builder: (context) => WarehouseLocationFormScreen(id: args),
              );
            } else {
              // 기본값
              return MaterialPageRoute(
                builder: (context) => const WarehouseLocationFormScreen(),
              );
            }
          
          // 재고 관리 관련 라우트
          case Routes.inventoryStockIn:
            return MaterialPageRoute(
              builder: (context) => const StockInForm(),
            );
          case Routes.inventoryStockOut:
            return MaterialPageRoute(
              builder: (context) => const StockOutForm(),
            );
          
          // 유지보수 관리 관련 라우트
          case Routes.maintenanceAdd:
            return MaterialPageRoute(
              builder: (context) => const Scaffold(
                body: Center(
                  child: MaintenanceFormDialog(),
                ),
              ),
            );
          case Routes.maintenanceEdit:
            final _ = settings.arguments as int;  // id will be used when edit mode is implemented
            // TODO: 수정 모드 구현 필요
            return MaterialPageRoute(
              builder: (context) => const Scaffold(
                body: Center(
                  child: MaintenanceFormDialog(),
                ),
              ),
            );
          
          default:
            return MaterialPageRoute(
              builder:
                  (context) => AppLayout(initialRoute: Routes.home),
            );
        }
        },
        // 전역 네비게이터 키 사용: 인터셉터 등에서 401 발생 시 로그인으로 전환
        navigatorKey: appNavigatorKey,
      ),
    );
  }
}
