import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_url.dart';
import 'package:niogu_ecommerce_v1/core/models/api_response_model.dart';
import 'package:niogu_ecommerce_v1/core/system/system_setting.dart';
import 'package:niogu_ecommerce_v1/features/order/data/models/order_model.dart';

abstract class IOrderRemoteDatasource {
  Future<ApiResponseModel<List<OrderReportResponse>>> fetchOrders();

  Future<ApiResponseModel<OrderInfoResponse>> fetchOrderById(String orderId);

  Future<ApiResponseModel<void>> cancelOrder(String orderId);

  Future<ApiResponseModel<void>> cancellationRequest(String orderId);

  Future<ApiResponseModel<List<ProductReviewItemResponse>>>
  fetchProductReviewItem(String orderId);

  Future<ApiResponseModel<void>> reviewProduct(
    String orderId,
    List<ProductReviewRequest> request,
  );
}

class OrderRemoteDatasourceImpl implements IOrderRemoteDatasource {
  final Dio _dio;

  OrderRemoteDatasourceImpl(this._dio);

  @override
  Future<ApiResponseModel<List<OrderReportResponse>>> fetchOrders() async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.get("${AppUrl.ORDERS}/customer/$customerId");

    return ApiResponseModel.fromJson(
      response.data,
      (json) => (json as List)
          .map(
            (item) =>
                OrderReportResponse.fromJson(item as Map<String, dynamic>),
          )
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<OrderInfoResponse>> fetchOrderById(
    String orderId,
  ) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.get(
      "${AppUrl.ORDERS}/customer/$customerId/$orderId",
    );

    return ApiResponseModel.fromJson(
      response.data,
      (json) => OrderInfoResponse.fromJson(json as Map<String, dynamic>),
    );
  }

  @override
  Future<ApiResponseModel<void>> cancelOrder(String orderId) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.patch(
      "${AppUrl.ORDERS}/$customerId/$orderId/cancel",
    );

    return ApiResponseModel.fromJson(response.data, (json) => null);
  }

  @override
  Future<ApiResponseModel<void>> cancellationRequest(String orderId) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.patch(
      "${AppUrl.ORDERS}/$customerId/$orderId/cancellation-request",
    );

    return ApiResponseModel.fromJson(response.data, (json) => null);
  }

  @override
  Future<ApiResponseModel<List<ProductReviewItemResponse>>>
  fetchProductReviewItem(String orderId) async {
    final response = await _dio.get("${AppUrl.REVIEW}/$orderId");

    return ApiResponseModel.fromJson(
      response.data,
      (json) => (json as List)
          .map((item) => ProductReviewItemResponse.fromJson(item))
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<void>> reviewProduct(
    String orderId,
    List<ProductReviewRequest> request,
  ) async {
    final response = await _dio.post(
      "${AppUrl.REVIEW}/$orderId",
      data: request.map((item) => item.toJson()).toList(),
      options: Options(contentType: Headers.jsonContentType),
    );

    return ApiResponseModel.fromJson(response.data, (json) => null);
  }
}
