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/order/data/datasource/order_remote_datasource_impl.dart';
import 'package:niogu_ecommerce_v1/features/order/domain/entities/order.dart';
import 'package:niogu_ecommerce_v1/features/order/domain/repositories/i_order_repository.dart';

class OrderRepositoryImpl implements IOrderRepository {
  final IOrderRemoteDatasource _datasource;

  OrderRepositoryImpl(this._datasource);

  @override
  Future<List<OrderReport>> fetchOrders() async {
    try {
      final response = await _datasource.fetchOrders();

      final data = response.data;

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

      if (data.isEmpty) return [];

      return data.map((order) => order.toOrderReport()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<OrderInfo?> fetchOrderById(String orderId) async {
    try {
      final response = await _datasource.fetchOrderById(orderId);

      final order = response.data;

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

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

  @override
  Future<void> cancelOrder(String orderId) async {
    try {
      await _datasource.cancelOrder(orderId);
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<void> cancellationRequest(String orderId) async {
    try {
      await _datasource.cancellationRequest(orderId);
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<List<ProductReviewItem>> fetchProductReviewItem(String orderId) async {
    try {
      final response = await _datasource.fetchProductReviewItem(orderId);

      final products = response.data;

      if (response.status != 'success' || products == null) return [];

      return products.map((product) => product.toProductReviewItem()).toList();
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }

  @override
  Future<void> reviewProduct(
    String orderId,
    List<ProductReview> reviews,
  ) async {
    try {
      await _datasource.reviewProduct(
        orderId,
        reviews.map((review) => review.toProductReviewRequest()).toList(),
      );
    } on DioException catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      throw ServerException("Terjadi kesalahan koneksi");
    }
  }
}
