import 'package:cargocalculaterapp/core/error/failure.dart';
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:dartz/dartz.dart';
import '../../../../core/error/exceptions.dart';
import '../../domain/repository/auth_repository.dart';
import '../data_source/remote/auth_remote_data_source.dart';

class AuthRepositoryImpl extends AuthRepository {
  final AuthRemoteDataSource remoteDataSource;

  AuthRepositoryImpl(this.remoteDataSource);

  @override
  Future<Either<Failure, LoginResponse>> login(LoginRequest request) async {
    try {
      final response = await remoteDataSource.login(request);
      return Right(response);
    } catch (e) {
      if (e is ServerException) {
        return Left(ServerFailure(message: e.message));
      }
      return Left(ServerFailure(message: e.toString()));
    }
  }

  @override
  Future<Either<Failure, SignUpResponse>> signUp(SignUpRequest request) async {
    try {
      final response = await remoteDataSource.signUp(request);
      return Right(response);
    } catch (e) {
      if (e is ServerException) {
        return Left(ServerFailure(message: e.message));
      }
      return Left(ServerFailure(message: e.toString()));
    }
  }

  @override
  Future<Either<Failure, AuthVerificationResponse>> verify(
    AuthVerificationRequest request,
  ) async {
    try {
      final response = await remoteDataSource.verify(request);
      return Right(response);
    } catch (e) {
      if (e is ServerException) {
        return Left(ServerFailure(message: e.message));
      }
      return Left(ServerFailure(message: e.toString()));
    }
  }

  @override
  Future<Either<Failure, FcmAddResponse>> fcmAdd(FcmAddRequest request) async {
    try {
      final response = await remoteDataSource.addFcm(request);
      return Right(response);
    } catch (e) {
      if (e is ServerException) {
        return Left(ServerFailure(message: e.message));
      }
      return Left(ServerFailure(message: e.toString()));
    }
  }
}
