import 'package:dio/dio.dart';
import 'package:frontend_eccp_mobile/app/core/config/app_environment.dart';
import 'package:frontend_eccp_mobile/app/core/network/api_endpoint.dart';
import 'package:frontend_eccp_mobile/app/core/utils/session_manager.dart';
import 'package:frontend_eccp_mobile/modules/auth/refresh_token/data/model/refresh_token_response.dart';
import 'package:injectable/injectable.dart';

@lazySingleton
class AuthRemoteDataSource {
  final Dio _dio = Dio(
    BaseOptions(
      baseUrl: AppEnvironment.baseUrl,
    ),
  );

  Future<RefreshTokenResponse> refreshToken() async {
    final token = await SessionManager.getRefreshToken();
    final response = await _dio.post<Map<String, dynamic>>(
      ApiEndpoint.refreshToken,
      data: {'refresh_token': token},
    );

    final data = response.data;
    if (data == null) {
      throw Exception('EMPTY_RESPONSE');
    }

    return RefreshTokenResponse.fromJson(data);
  }
}
