import 'dart:convert';
import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:http/http.dart' as http;
import 'package:initial_folder/models/zero_price_model.dart';

class PaymentServices {
  Uri _freeUrl = Uri.parse('$baseUrl/users/payment/free-course');
  Uri _freeUrlCoupon = Uri.parse('$baseUrl/users/coupon-payment-free');
  Uri _paidurl = Uri.parse("$baseUrl/payment/charge");
  Uri _zeroPay = Uri.parse("$baseUrl/payment/zero-price-payment");

  Future<Map<String, String>> getSnapTransactionToken({
    required String orderId,
    required int grossAmount,
    required List<Map<String, dynamic>> dataInvoice,
  }) async {
    String? token = await UsersInfo().getToken(); 

    var headers = {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer $token',
    };

    var body = jsonEncode({
      "code_coupon": null,  
      "code_referal": null, 
      "data_invoice": dataInvoice,  
      "total_payment": grossAmount,
    });

    print("Request Body: $body");

    var response = await http.post(_paidurl, headers: headers, body: body);

    if (response.statusCode == 201) {
      var responseData = jsonDecode(response.body);
      String transactionToken = responseData['token'];
      String redirectUrl = responseData['redirect_url']; 
      
      return {
        'transactionToken': transactionToken,
        'redirect_url': redirectUrl,
      };
    } else {
      print("Gagal mendapatkan token transaksi: ${response.body}");
      throw Exception('Gagal mendapatkan token transaksi');
    }
  }

  Future<bool> freeCoure(int idCourse) async {
    String? token = await UsersInfo().getToken();

    var body = jsonEncode({
      "course_id": idCourse,
    });

    http.Response response = await http.post(_freeUrl, headers: headerWithToken(token!), body: body);
    print(response.body);

    if (response.statusCode == 201) {
      print("Berhasil bayar kursus gratis: ${response.body}");
      return true;
    } else if (response.statusCode == 400) {
      print("Gagal bayar gratis: ${response.body}");
      return false;
    } else {
      throw Exception('Gagal Membeli kursus');
    }
  }

  Future<bool> freeCoureCoupon(int idCourse, String coupon) async {
    String? token = await UsersInfo().getToken();

    var body = jsonEncode({
      "course_id": idCourse,
      "coupon": coupon,
    });

    http.Response response = await http.post(_freeUrlCoupon, headers: headerWithToken(token!), body: body);
    
    if (response.statusCode == 201) {
      print("Berhasil bayar kupon gratis: ${response.body}");
      return true;
    } else if (response.statusCode == 400) {
      print("Gagal bayar kupon gratis: ${response.body}");
      return false;
    } else {
      throw Exception('Gagal bayar kupon gratis');
    }
  }

  Future<List<ZeroPrice>> zeroPayment({
    required List<Map<String, String>> dataInvoice,
    required String totalPayment,
  }) async {
    String? token = await UsersInfo().getToken();

    var body = jsonEncode({
      "data_invoice": dataInvoice,
      "total_payment": totalPayment,
    });

    http.Response response = await http.post(_zeroPay, headers: headerWithToken(token!), body: body);
    print(response.body);

    List<ZeroPrice> zeroPrice = [];
    if (response.statusCode == 201) {
      zeroPrice.add(ZeroPrice.fromJson(jsonDecode(response.body)));
      return zeroPrice;
    } else if (response.statusCode == 404) {
      return [];
    } else {
      throw Exception('Gagal Mendapatkan kursus');
    }
  }
}
