import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:flutter/foundation.dart';
import '../../../../core/constants/api_endpoints.dart';
import '../../../../services/auth_service.dart';
import '../../../../core/config/environment.dart';

/// 인증 인터셉터
class AuthInterceptor extends Interceptor {
  AuthService? _authService;
  final Dio dio;
  
  AuthInterceptor(this.dio);
  
  AuthService? get authService {
    try {
      _authService ??= GetIt.instance<AuthService>();
      return _authService;
    } catch (e) {
      if (kDebugMode) {
        debugPrint('Failed to get AuthService in AuthInterceptor: $e');
      }
      return null;
    }
  }
  
  @override
  void onRequest(
    RequestOptions options,
    RequestInterceptorHandler handler,
  ) async {
    if (Environment.enableLogging && kDebugMode) {
      debugPrint('[AuthInterceptor] onRequest: ${options.method} ${options.path}');
    }
    
    // 로그인, 토큰 갱신 요청은 토큰 없이 진행
    if (_isAuthEndpoint(options.path)) {
      if (Environment.enableLogging && kDebugMode) {
        debugPrint('[AuthInterceptor] Auth endpoint detected, skipping token attachment');
      }
      handler.next(options);
      return;
    }
    
    // 저장된 액세스 토큰 가져오기
    final service = authService;
    if (Environment.enableLogging && kDebugMode) {
      debugPrint('[AuthInterceptor] AuthService available: ${service != null}');
    }
    
    if (service != null) {
      final accessToken = await service.getAccessToken();
      if (Environment.enableLogging && kDebugMode) {
        debugPrint('[AuthInterceptor] Access token retrieved: ${accessToken != null ? 'Yes (${accessToken.substring(0, 10)}...)' : 'No'}');
      }
      
      if (accessToken != null) {
        options.headers['Authorization'] = 'Bearer $accessToken';
        if (Environment.enableLogging && kDebugMode) {
          debugPrint('[AuthInterceptor] Authorization header set: Bearer ${accessToken.substring(0, 10)}...');
        }
      } else {
        if (Environment.enableLogging && kDebugMode) {
          debugPrint('[AuthInterceptor] WARNING: No access token available for protected endpoint');
        }
      }
    } else {
      if (Environment.enableLogging && kDebugMode) {
        debugPrint('[AuthInterceptor] ERROR: AuthService not available from GetIt');
      }
    }
    
    if (Environment.enableLogging && kDebugMode) {
      debugPrint('[AuthInterceptor] Final headers: ${options.headers}');
    }
    handler.next(options);
  }
  
  @override
  void onError(
    DioException err,
    ErrorInterceptorHandler handler,
  ) async {
    if (Environment.enableLogging && kDebugMode) {
      debugPrint('[AuthInterceptor] onError: ${err.response?.statusCode} ${err.message}');
    }
    
    // 401 Unauthorized 에러 처리
    if (err.response?.statusCode == 401) {
      // 인증 관련 엔드포인트는 재시도하지 않음
      if (_isAuthEndpoint(err.requestOptions.path)) {
        if (Environment.enableLogging && kDebugMode) {
          debugPrint('[AuthInterceptor] Auth endpoint 401 error, skipping retry');
        }
        handler.next(err);
        return;
      }
      
      final service = authService;
      if (service != null) {
        if (Environment.enableLogging && kDebugMode) {
          debugPrint('[AuthInterceptor] Attempting token refresh...');
        }
        // 토큰 갱신 시도
        final refreshResult = await service.refreshToken();
        
        final refreshSuccess = refreshResult.fold(
          (failure) {
            if (Environment.enableLogging && kDebugMode) {
              debugPrint('[AuthInterceptor] Token refresh failed: ${failure.message}');
            }
            return false;
          },
          (tokenResponse) {
            if (Environment.enableLogging && kDebugMode) {
              debugPrint('[AuthInterceptor] Token refresh successful');
            }
            return true;
          },
        );
        
        if (refreshSuccess) {
          // 새로운 토큰으로 원래 요청 재시도
          try {
            final newAccessToken = await service.getAccessToken();
            
            if (newAccessToken != null) {
              if (Environment.enableLogging && kDebugMode) {
                debugPrint('[AuthInterceptor] Retrying request with new token');
              }
              err.requestOptions.headers['Authorization'] = 'Bearer $newAccessToken';
              
              // dio 인스턴스를 통해 재시도
              final response = await dio.fetch(err.requestOptions);
              handler.resolve(response);
              return;
            }
          } catch (e) {
            if (Environment.enableLogging && kDebugMode) {
              debugPrint('[AuthInterceptor] Request retry failed: $e');
            }
            // 재시도 실패
            handler.next(err);
            return;
          }
        }
        
        // 토큰 갱신 실패 시 로그인 화면으로 이동
        if (Environment.enableLogging && kDebugMode) {
          debugPrint('[AuthInterceptor] Clearing session due to auth failure');
        }
        await service.clearSession();
        // TODO: Navigate to login screen
      }
    }
    
    handler.next(err);
  }
  
  
  /// 인증 관련 엔드포인트 확인
  bool _isAuthEndpoint(String path) {
    return path.contains(ApiEndpoints.login) ||
           path.contains(ApiEndpoints.refresh) ||
           path.contains(ApiEndpoints.logout);
  }
}