

import 'package:dartz/dartz.dart';
import 'package:hmg_patient_app_new/features/qr_parking/models/qr_parking_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 QrParkingRepo {
  Future<Either<Failure, GenericApiModel<List<QrParkingResponseModel>>>>
  getQrParking({
    required int qrParkingId,
  });
}

class QrParkingRepoImp implements QrParkingRepo {
  final ApiClient apiClient;
  final LoggerService loggerService;

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

  @override
  Future<Either<Failure, GenericApiModel<List<QrParkingResponseModel>>>>
  getQrParking({required int qrParkingId}) async {
    try {
      GenericApiModel<List<QrParkingResponseModel>>? apiResponse;
      Failure? failure;

      await apiClient.post(
        ApiConsts.getQrParkingDetails, // GetQRParkingByID
        body: {'QRParkingID': qrParkingId},
        onFailure: (error, statusCode,
            {messageStatus, failureType}) {
          failure = failureType ??
              StatusCodeFailure("$error ($statusCode)");
        },
        onSuccess: (response, statusCode,
            {messageStatus, errorMessage}) {
          final list =
              (response['List_SWP_QRParkingModel'] as List?) ?? [];

          final res = list
              .map((e) => QrParkingResponseModel.fromJson(
            Map<String, dynamic>.from(e),
          ))
              .toList();

          apiResponse = GenericApiModel<List<QrParkingResponseModel>>(
            messageStatus: messageStatus,
            statusCode: statusCode,
            errorMessage: null,
            data: res,
          );
        },
      );

      if (failure != null) return Left(failure!);
      if (apiResponse == null) {
        return Left(ServerFailure("Unknown error"));
      }

      return Right(apiResponse!);
    } catch (e) {
      return Left(UnknownFailure(e.toString()));
    }
  }
}

