import 'package:injectable/injectable.dart';
import 'package:krow/core/application/clients/api/api_client.dart';
import 'package:krow/core/application/clients/api/api_exception.dart';
import 'package:krow/core/sevices/create_event_service/data/create_event_service_gql.dart';
import 'package:krow/core/sevices/create_event_service/data/models/create_event_input_model.dart';

@Injectable()
class CreateEventServiceApiProvider {
  final ApiClient _client;

  CreateEventServiceApiProvider({required ApiClient client}) : _client = client;

  Future<String> createEvent(CreateEventInputModel input) async {
    var result = await _client.mutate(
      schema: createEventMutation,
      body: {
        'input': input.toJson()..remove('id'),
      },
    );

    if (result.hasException) {
      throw parseBackendError(result.exception);
    }

    return result.data?['client_create_event']['id'] ?? '';
  }

  Future<void> deleteDraft(String id) async {
    var result = await _client.mutate(
      schema: deleteDraftMutation,
      body: {
        'id': id,
      },
    );

    if (result.hasException) {
      print(result.exception);
      throw parseBackendError(result.exception);
    }
  }

  Future<void> publishEvent(String id) async {
    var result = await _client.mutate(
      schema: publishDraftMutation,
      body: {
        'id': id,
      },
    );

    if (result.hasException) {
      throw parseBackendError(result.exception);
    }
  }

  Future<void> updateEvent(CreateEventInputModel input) async {
    var result = await _client.mutate(
      schema: updateEventMutation,
      body: {
        'input': input.toJson(),
      },
    );

    if (result.hasException) {
      throw parseBackendError(result.exception);
    }
  }
}
