import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/errors/exceptions.dart';
import 'package:niogu_ecommerce_v1/core/utils/log_message.dart';
import 'package:niogu_ecommerce_v1/features/checkout/data/datasource/checkout_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/checkout/domain/entities/checkout.dart';
import 'package:niogu_ecommerce_v1/features/checkout/domain/repositories/i_checkout_repository.dart';

class CheckoutRepositoryImpl implements ICheckoutRepository {
  final ICheckoutRemoteDatasource _datasource;

  CheckoutRepositoryImpl(this._datasource);

  @override
  Future<Checkout?> checkout(OnlineOrder order) async {
    try {
      final response = await _datasource.checkout(order.toOnlineOrderRequest());

      final data = response.data;
      
      if (response.status != 'success' || data == null) return null;

      return data.toCheckout();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }
}
