

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

abstract class ActivePrescriptionsRepo {

  Future<Either<Failure, GenericApiModel<List<ActivePrescriptionsResponseModel>>>> getActivePrescriptionsDetails();

}

class ActivePrescriptionsRepoImp implements ActivePrescriptionsRepo {
  final ApiClient apiClient;
  final LoggerService loggerService;

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

   @override

   Future<Either<Failure, GenericApiModel<List<ActivePrescriptionsResponseModel>>>> getActivePrescriptionsDetails() async
   {
     try {
       GenericApiModel<List<ActivePrescriptionsResponseModel>>? apiResponse;
       Failure? failure;
       await apiClient.post(
         ApiConsts.getActivePrescriptionsDetails,
         body: {},
         onFailure: (error, statusCode, {messageStatus, failureType}) {
           failure = failureType;
         },
         onSuccess: (response, statusCode, {messageStatus, errorMessage}) {
           try {
             var list = response['List_ActiveGetPrescriptionReportByPatientID'];
             var res = list
                 .map<ActivePrescriptionsResponseModel>(
                     (item) => ActivePrescriptionsResponseModel.fromJson(item))
                 .toList();

             apiResponse = GenericApiModel<List<ActivePrescriptionsResponseModel>>(
               messageStatus: messageStatus,
               statusCode: statusCode,
               errorMessage: null,
             //  data: response,
               data: res
             );
            return apiResponse;
           } 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()));
     }
   }

}