import '../../core/constants/api_endpoints.dart';
import '../../core/network/dio_client.dart';
import '../models/order_models.dart';

/// 充提服务
class FundService {
  final DioClient _client;

  FundService(this._client);

  /// 申请充值
  Future<ApiResponse<Map<String, dynamic>>> deposit({
    required String amount,
    String? remark,
  }) async {
    return _client.post<Map<String, dynamic>>(
      ApiEndpoints.deposit,
      data: {
        'amount': amount,
        if (remark != null) 'remark': remark,
      },
    );
  }

  /// 申请提现
  Future<ApiResponse<Map<String, dynamic>>> withdraw({
    required String amount,
    String? remark,
  }) async {
    return _client.post<Map<String, dynamic>>(
      ApiEndpoints.withdraw,
      data: {
        'amount': amount,
        if (remark != null) 'remark': remark,
      },
    );
  }

  /// 取消订单
  Future<ApiResponse<void>> cancelOrder(String orderNo) async {
    return _client.post<void>(
      ApiEndpoints.cancelOrder,
      data: {'orderNo': orderNo},
    );
  }

  /// 获取充提记录
  Future<ApiResponse<Map<String, dynamic>>> getOrders({
    int? type,
    int pageNum = 1,
    int pageSize = 20,
  }) async {
    final params = <String, dynamic>{
      'pageNum': pageNum,
      'pageSize': pageSize,
    };
    if (type != null) params['type'] = type;

    return _client.get<Map<String, dynamic>>(
      ApiEndpoints.fundOrders,
      queryParameters: params,
    );
  }

  /// 获取充提订单详情
  Future<ApiResponse<OrderFund>> getOrderDetail(String orderNo) async {
    final response = await _client.get<Map<String, dynamic>>(
      ApiEndpoints.fundOrders,
      queryParameters: {'orderNo': orderNo},
    );

    if (response.success && response.data != null) {
      return ApiResponse.success(
        OrderFund.fromJson(response.data!),
        response.message,
      );
    }
    return ApiResponse.fail(response.message ?? '获取订单详情失败');
  }
}
