import '../../models/trade_order_model.dart';
import '../../models/kline_model.dart';
import '../../../core/network/api_client.dart';
import '../../../core/network/api_endpoints.dart';
import '../../../core/error/exceptions.dart';

abstract class TradingRemoteDataSource {
  Future<String> getCurrentPrice();
  Future<List<KlineModel>> getKlineData(String period);
  Future<TradeOrderModel> buyShares({
    required String accountSequence,
    required String amount,
  });
  Future<TradeOrderModel> sellShares({
    required String accountSequence,
    required String amount,
  });
  Future<List<TradeOrderModel>> getOrders(
    String accountSequence, {
    int page = 1,
    int limit = 20,
  });
  Future<void> transfer({
    required String accountSequence,
    required String amount,
    required String direction,
  });
}

class TradingRemoteDataSourceImpl implements TradingRemoteDataSource {
  final ApiClient client;

  TradingRemoteDataSourceImpl({required this.client});

  @override
  Future<String> getCurrentPrice() async {
    try {
      final response = await client.get(ApiEndpoints.currentPrice);
      return response.data['price']?.toString() ?? '0';
    } catch (e) {
      throw ServerException(e.toString());
    }
  }

  @override
  Future<List<KlineModel>> getKlineData(String period) async {
    try {
      final response = await client.get(
        ApiEndpoints.klineData,
        queryParameters: {'period': period},
      );
      final items = response.data as List? ?? [];
      return items.map((json) => KlineModel.fromJson(json)).toList();
    } catch (e) {
      throw ServerException(e.toString());
    }
  }

  @override
  Future<TradeOrderModel> buyShares({
    required String accountSequence,
    required String amount,
  }) async {
    try {
      final response = await client.post(
        ApiEndpoints.createOrder(accountSequence),
        data: {'orderType': 'BUY', 'quantity': amount},
      );
      return TradeOrderModel.fromJson(response.data);
    } catch (e) {
      throw ServerException(e.toString());
    }
  }

  @override
  Future<TradeOrderModel> sellShares({
    required String accountSequence,
    required String amount,
  }) async {
    try {
      final response = await client.post(
        ApiEndpoints.createOrder(accountSequence),
        data: {'orderType': 'SELL', 'quantity': amount},
      );
      return TradeOrderModel.fromJson(response.data);
    } catch (e) {
      throw ServerException(e.toString());
    }
  }

  @override
  Future<List<TradeOrderModel>> getOrders(
    String accountSequence, {
    int page = 1,
    int limit = 20,
  }) async {
    try {
      final response = await client.get(
        ApiEndpoints.orders(accountSequence),
        queryParameters: {'page': page, 'pageSize': limit},
      );
      final items = response.data['items'] as List? ?? [];
      return items.map((json) => TradeOrderModel.fromJson(json)).toList();
    } catch (e) {
      throw ServerException(e.toString());
    }
  }

  @override
  Future<void> transfer({
    required String accountSequence,
    required String amount,
    required String direction,
  }) async {
    try {
      await client.post(
        ApiEndpoints.transfer(accountSequence),
        data: {'amount': amount, 'direction': direction},
      );
    } catch (e) {
      throw ServerException(e.toString());
    }
  }
}
