import 'package:dio/dio.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/data/model/billings_history_model.dart';
import 'package:hoshan/data/model/discount_model.dart';
import 'package:hoshan/data/model/plans_model.dart';

class PaymantRepository {
  static final DioService _dioService = DioService();

  static Future<String> getLinkPaymant(int amount, {final String? code}) async {
    try {
      Map<String, dynamic>? queryParameters = {'amount': amount};
      if (code != null && code.isNotEmpty) {
        queryParameters.addAll({'code': code});
      }
      Response response = await _dioService
          .sendRequest()
          .get(DioService.paymant, queryParameters: queryParameters);
      return response.data['url'];
    } catch (ex) {
      rethrow;
    }
  }

  static Future<List<Billings>> getPaymantHistory() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.paymantHistory);
      return BillingsHistoryModel.fromJson(response.data).billings!;
    } catch (ex) {
      rethrow;
    }
  }

  static Future<List<Plans>> getPaymantPlans() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.paymantPlans);
      return PlansModel.fromJson(response.data).plans!;
    } catch (ex) {
      rethrow;
    }
  }

  static Future<DiscountModel> getDiscount(String code, String prId) async {
    try {
      Response response = await _dioService
          .sendRequest()
          .post(DioService.discount, data: {"code": code, 'product_id': prId});
      return DiscountModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<String> setSettlement(bool isRial) async {
    try {
      Response response = await _dioService.sendRequest().post(
          DioService.settlement,
          data: {"type": isRial ? 'money' : 'coin'});
      return response.data['msg'];
    } catch (ex) {
      rethrow;
    }
  }
}
