import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_request.dart';
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_response.dart';
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_request.dart';
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_response.dart';
import 'package:cargocalculaterapp/features/auth/data/model/login_request.dart';

import 'package:cargocalculaterapp/features/auth/data/model/login_response.dart';
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_request.dart';
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_response.dart';
import 'package:dio/dio.dart';

import '../../../../../constants/constants.dart';
import '../../../../../core/error/exceptions.dart';
import '../../../../../core/local_source/local_source.dart';
import '../../../../../injector_container.dart';
import 'auth_remote_data_source.dart';

class AuthRemoteDataSourceImpl extends AuthRemoteDataSource {
  final Dio dio;

  AuthRemoteDataSourceImpl(this.dio);

  @override
  Future<LoginResponse> login(LoginRequest request) async {
    try {
      final Response response = await dio.post(
        Constants.baseUrl + Urls.login,
        options: DioConstants.options,
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return LoginResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<SignUpResponse> signUp(SignUpRequest request) async {
    try {
      final Response response = await dio.post(
        Constants.baseUrl + Urls.signUp,
        options: DioConstants.options,
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return SignUpResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);} on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<AuthVerificationResponse> verify(
    AuthVerificationRequest request,
  ) async {
    try {
      final Response response = await dio.post(
        Constants.baseUrl + Urls.verify,
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return AuthVerificationResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }

  @override
  Future<FcmAddResponse> addFcm(FcmAddRequest request) async {
    try {
      final Response response = await dio.put(
        Constants.baseUrl + Urls.addFcm,
        options: DioConstants.options
          ..headers = {
            "Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
          },
        data: request,
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        return FcmAddResponse.fromJson(response.data);
      }
      throw ServerException.fromJson(response.data);
    } on DioException catch (e) {
      throw ServerException.fromJson(e.response?.data);
    } on FormatException {
      throw ServerException(message: Validations.someThingWentWrong);
    }
  }
}
