import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:hoshan/data/model/discount_model.dart';
import 'package:hoshan/data/repository/paymant_repository.dart';

part 'discount_state.dart';

class DiscountCubit extends Cubit<DiscountState> {
  DiscountCubit() : super(DiscountInitial());

  void verifyDiscount(String code, String id) async {
    emit(DiscountLoading());
    try {
      final response = await PaymantRepository.getDiscount(code, id);
      emit(DiscountSuccess(discount: response, code: code));
    } on DioException catch (e) {
      try {
        emit(DiscountFail(message: e.response?.data['detail']));
      } catch (e) {
        emit(const DiscountFail());
      }
      if (kDebugMode) {
        print('Dio Error is: $e');
      }
    }
  }

  Future refresh() async {
    emit(DiscountInitial());
  }
}
