import 'package:dartz/dartz.dart';
import 'package:hmg_patient_app_new/core/api/api_client.dart';
import 'package:hmg_patient_app_new/core/api_consts.dart';
import 'package:hmg_patient_app_new/core/common_models/generic_api_model.dart';
import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart';
import 'package:hmg_patient_app_new/features/offers_and_discounts/models/offers_and_discounts_response_model.dart';
import 'package:hmg_patient_app_new/services/logger_service.dart';

abstract class OffersAndDiscountsRepo {
  Future<Either<Failure, GenericApiModel<List<OffersAndDiscountsResponseModel>>>> getOffersAndDiscounts();
  Future<Either<Failure, GenericApiModel<List<OffersAndDiscountsResponseModel>>>> getOffersAndDiscountsHistory();
}

class OffersAndDiscountsRepoImp implements OffersAndDiscountsRepo {
  final ApiClient apiClient;
  final LoggerService loggerService;

  OffersAndDiscountsRepoImp({required this.loggerService, required this.apiClient});

  @override
  Future<Either<Failure, GenericApiModel<List<OffersAndDiscountsResponseModel>>>> getOffersAndDiscounts() async {
    Map<String, dynamic> mapDevice = {};

    try {
      GenericApiModel<List<OffersAndDiscountsResponseModel>>? apiResponse;
      Failure? failure;
      await apiClient.post(
        GET_OFFERS_AND_DISCOUNTS, // TODO: Replace with actual API endpoint
        body: mapDevice,
        onFailure: (error, statusCode, {messageStatus, failureType}) {
          failure = failureType;
        },
        onSuccess: (response, statusCode, {messageStatus, errorMessage}) {
          try {
            final list = response['List_OffersAndDiscounts'];
            if (list == null || list.isEmpty) {
              throw Exception("offers and discounts list is empty");
            }

            final offers = list.map((item) => OffersAndDiscountsResponseModel.fromJson(item as Map<String, dynamic>)).toList().cast<OffersAndDiscountsResponseModel>();

            apiResponse = GenericApiModel<List<OffersAndDiscountsResponseModel>>(
              messageStatus: messageStatus,
              statusCode: statusCode,
              errorMessage: null,
              data: offers,
            );
          } catch (e) {
            failure = DataParsingFailure(e.toString());
          }
        },
      );
      if (failure != null) return Left(failure!);
      if (apiResponse == null) return Left(ServerFailure("Unknown error"));
      return Right(apiResponse!);
    } catch (e) {
      return Left(UnknownFailure(e.toString()));
    }
  }

  @override
  Future<Either<Failure, GenericApiModel<List<OffersAndDiscountsResponseModel>>>> getOffersAndDiscountsHistory() async {
    Map<String, dynamic> mapDevice = {};

    try {
      GenericApiModel<List<OffersAndDiscountsResponseModel>>? apiResponse;
      Failure? failure;
      await apiClient.post(
        GET_OFFERS_AND_DISCOUNTS_HISTORY, // TODO: Replace with actual API endpoint
        body: mapDevice,
        onFailure: (error, statusCode, {messageStatus, failureType}) {
          failure = failureType;
        },
        onSuccess: (response, statusCode, {messageStatus, errorMessage}) {
          try {
            final list = response['List_OffersAndDiscountsHistory'];
            if (list == null || list.isEmpty) {
              throw Exception("offers and discounts history list is empty");
            }

            final historyOffers = list.map((item) => OffersAndDiscountsResponseModel.fromJson(item as Map<String, dynamic>)).toList().cast<OffersAndDiscountsResponseModel>();

            apiResponse = GenericApiModel<List<OffersAndDiscountsResponseModel>>(
              messageStatus: messageStatus,
              statusCode: statusCode,
              errorMessage: null,
              data: historyOffers,
            );
          } catch (e) {
            failure = DataParsingFailure(e.toString());
          }
        },
      );
      if (failure != null) return Left(failure!);
      if (apiResponse == null) return Left(ServerFailure("Unknown error"));
      return Right(apiResponse!);
    } catch (e) {
      return Left(UnknownFailure(e.toString()));
    }
  }
}

