import 'package:dartz/dartz.dart';
import '../../domain/entities/contribution.dart';
import '../../domain/entities/contribution_record.dart';
import '../../domain/entities/contribution_stats.dart';
import '../../domain/repositories/contribution_repository.dart';
import '../../core/error/exceptions.dart';
import '../../core/error/failures.dart';
import '../datasources/remote/contribution_remote_datasource.dart';

class ContributionRepositoryImpl implements ContributionRepository {
  final ContributionRemoteDataSource remoteDataSource;

  ContributionRepositoryImpl({required this.remoteDataSource});

  @override
  Future<Either<Failure, Contribution>> getUserContribution(String accountSequence) async {
    try {
      final result = await remoteDataSource.getUserContribution(accountSequence);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, ContributionRecordsPage>> getContributionRecords(
    String accountSequence, {
    ContributionSourceType? sourceType,
    bool includeExpired = false,
    int page = 1,
    int pageSize = 50,
  }) async {
    try {
      final result = await remoteDataSource.getContributionRecords(
        accountSequence,
        sourceType: sourceType,
        includeExpired: includeExpired,
        page: page,
        pageSize: pageSize,
      );
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, ContributionStats>> getContributionStats() async {
    try {
      final result = await remoteDataSource.getContributionStats();
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }
}
