/// User Info Repository Implementation
///
/// Implements the UserInfoRepository interface with API integration.
library;

import 'package:worker/core/errors/exceptions.dart';
import 'package:worker/features/account/data/datasources/user_info_remote_datasource.dart';
import 'package:worker/features/account/domain/entities/user_info.dart';
import 'package:worker/features/account/domain/repositories/user_info_repository.dart';

/// User Info Repository Implementation
///
/// Handles user information operations with:
/// - API data fetching via remote datasource
/// - Error handling and transformation
/// - Entity conversion
class UserInfoRepositoryImpl implements UserInfoRepository {
  UserInfoRepositoryImpl({
    required this.remoteDataSource,
  });

  final UserInfoRemoteDataSource remoteDataSource;

  // =========================================================================
  // GET USER INFO
  // =========================================================================

  @override
  Future<UserInfo> getUserInfo() async {
    try {
      _debugPrint('Fetching user info from API');

      // Fetch from remote datasource
      final userInfoModel = await remoteDataSource.getUserInfo();

      _debugPrint('Successfully fetched user info: ${userInfoModel.fullName}');

      // Convert model to entity
      return userInfoModel.toEntity();
    } on UnauthorizedException catch (e) {
      _debugPrint('Unauthorized error: $e');
      rethrow;
    } on NotFoundException catch (e) {
      _debugPrint('Not found error: $e');
      rethrow;
    } on ServerException catch (e) {
      _debugPrint('Server error: $e');
      rethrow;
    } on NetworkException catch (e) {
      _debugPrint('Network error: $e');
      rethrow;
    } catch (e) {
      _debugPrint('Unexpected error: $e');
      throw ServerException('Failed to get user info: $e');
    }
  }

  // =========================================================================
  // REFRESH USER INFO
  // =========================================================================

  @override
  Future<UserInfo> refreshUserInfo() async {
    try {
      _debugPrint('Refreshing user info from API');

      // Fetch fresh data from remote datasource
      final userInfoModel = await remoteDataSource.refreshUserInfo();

      _debugPrint('Successfully refreshed user info: ${userInfoModel.fullName}');

      // Convert model to entity
      return userInfoModel.toEntity();
    } on UnauthorizedException catch (e) {
      _debugPrint('Unauthorized error on refresh: $e');
      rethrow;
    } on NotFoundException catch (e) {
      _debugPrint('Not found error on refresh: $e');
      rethrow;
    } on ServerException catch (e) {
      _debugPrint('Server error on refresh: $e');
      rethrow;
    } on NetworkException catch (e) {
      _debugPrint('Network error on refresh: $e');
      rethrow;
    } catch (e) {
      _debugPrint('Unexpected error on refresh: $e');
      throw ServerException('Failed to refresh user info: $e');
    }
  }

  // =========================================================================
  // UPDATE USER INFO
  // =========================================================================

  @override
  Future<UserInfo> updateUserInfo(Map<String, dynamic> data) async {
    try {
      _debugPrint('Updating user info via API');
      _debugPrint('Update data: $data');

      // Update via remote datasource (will fetch fresh data after update)
      final userInfoModel = await remoteDataSource.updateUserInfo(data);

      _debugPrint('Successfully updated user info: ${userInfoModel.fullName}');

      // Convert model to entity
      return userInfoModel.toEntity();
    } on UnauthorizedException catch (e) {
      _debugPrint('Unauthorized error on update: $e');
      rethrow;
    } on NotFoundException catch (e) {
      _debugPrint('Not found error on update: $e');
      rethrow;
    } on ServerException catch (e) {
      _debugPrint('Server error on update: $e');
      rethrow;
    } on NetworkException catch (e) {
      _debugPrint('Network error on update: $e');
      rethrow;
    } catch (e) {
      _debugPrint('Unexpected error on update: $e');
      throw ServerException('Failed to update user info: $e');
    }
  }
}

// ============================================================================
// DEBUG UTILITIES
// ============================================================================

/// Debug print helper
void _debugPrint(String message) {
  // ignore: avoid_print
  print('[UserInfoRepository] $message');
}
