import '../../domain/entities/promo_code_result.dart';
import '../../domain/repositories/promo_code_repository.dart';
import '../models/promo_code_response_dto.dart';
import '../network/api_service.dart';

class PromoCodeRepositoryImpl implements PromoCodeRepository {
  final ApiService apiService;

  PromoCodeRepositoryImpl(this.apiService);

  @override
  Future<PromoCodeResult> applyPromoCode(String code) async {
    try {
      final data = await apiService.applyPromoCode(code);
      final dto = PromoCodeResponseDto.fromJson(data);
      return dto.toEntity();
    } on PromoCodeNotFoundException {
      throw Exception('Промокод не найден');
    } catch (e) {
      throw Exception('Ошибка активации промокода: $e');
    }
  }
}