import 'package:dio/dio.dart';
import 'package:frontend_eccp_mobile/app/core/constants/dio_constant.dart';
import 'package:frontend_eccp_mobile/app/core/network/api_endpoint.dart';
import 'package:frontend_eccp_mobile/app/core/network/dio_client.dart';
import 'package:frontend_eccp_mobile/modules/profile/data/model/logout/logout_response.dart';
import 'package:injectable/injectable.dart';

@lazySingleton
class ProfileRemoteDataSource {
  final Dio _dio = DioClient.getInstance();

  Future<LogoutResponse> logout() async {
    try {
      final response = await _dio.post<Map<String, dynamic>>(
        ApiEndpoint.logout,
        options: Options(
          extra: {DioExtraKey.noAuth: false},
        ),
      );

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

      return LogoutResponse.fromJson(data);
    } on DioException catch (e) {
      final responseData = e.response?.data;
      if (responseData is Map<String, dynamic>) {
        return LogoutResponse.fromJson(responseData);
      }
      throw Exception(e.error?.toString() ?? 'LOGOUT_FAILED');
    }
  }
}
