import 'dart:convert';
import 'dart:developer';

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

class CouponService {
  Future<dynamic> getDiscountCourse(coupon) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/exchange-coupon?voucher=$coupon');
    print("Ini url api ${url}");
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    if (response.statusCode == 200) {
      print("Berhasil kupon ${response.body}");
      Map body = jsonDecode(response.body);
      var data = body['data'];
      List<DiscountCourseModel> course = [];
      String message = '';

      if (data is List) {
        for (var item in data) {
          course.add(DiscountCourseModel.fromJson(item));
        }
        return course;
      } else {
        message = data['message'];
        return message;
      }
    } else {
      print("Gagal kupon ${response.body}");
      throw Exception('Data Kursus kupon gagal diambil');
    }
  }
}
