import 'package:graphql/client.dart';
import 'package:injectable/injectable.dart';
import 'package:krow/core/application/clients/api/api_client.dart';
import 'package:krow/core/data/models/event/addon_model.dart';
import 'package:krow/core/data/models/event/business_member_model.dart';
import 'package:krow/core/data/models/event/hub_model.dart';
import 'package:krow/core/data/models/event/tag_model.dart';
import 'package:krow/core/data/models/shift/business_skill_model.dart';
import 'package:krow/core/data/models/shift/department_model.dart';
import 'package:krow/features/create_event/data/create_event_gql.dart';

@Injectable()
class CreateEventApiProvider {
  final ApiClient _client;

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

  Future<List<HubModel>> getHubs() async {
    QueryResult result = await _client.query(
      schema: getClientHubsQuery,
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }

    if (result.data == null || result.data!['client_hubs'] == null) {
      return [];
    }

    return (result.data!['client_hubs'] as List)
        .map((e) => HubModel.fromJson(e))
        .toList();
  }

  Future<List<AddonModel>> getAddons() async {
    QueryResult result = await _client.query(
      schema: getClientAddonsQuery,
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }

    if (result.data == null || result.data!['client_business_addons'] == null) {
      return [];
    }

    return (result.data!['client_business_addons'] as List)
        .map((e) => AddonModel.fromJson(e))
        .toList();
  }

  Future<List<TagModel>> getTags() async {
    QueryResult result = await _client.query(
      schema: getClientTagsQuery,
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }

    if (result.data == null || result.data!['client_tags'] == null) {
      return [];
    }

    return (result.data!['client_tags'] as List)
        .map((e) => TagModel.fromJson(e))
        .toList();
  }

  Future<List<BusinessMemberModel>> getContacts() async {
    QueryResult result = await _client.query(
      schema: getClientMembersQuery,
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }

    if (result.data == null || result.data!['client_shift_contacts'] == null) {
      return [];
    }

    return (result.data!['client_shift_contacts'] as List)
        .map((e) => BusinessMemberModel.fromJson(e))
        .toList();
  }

  Future<List<BusinessSkillModel>> getSkill() async {
    QueryResult result = await _client.query(
      schema: getClientBusinessSkillQuery,
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }

    if (result.data == null || result.data!['client_business_skills'] == null) {
      return [];
    }

    return (result.data!['client_business_skills'] as List)
        .map((e) => BusinessSkillModel.fromJson(e))
        .toList();
  }

  Future<List<DepartmentModel>> getDepartments(String hubId) async {
    QueryResult result = await _client.query(
      schema: getClientDepartmentsQuery,
      body: {'hub_id': hubId},
    );
    if (result.hasException) {
      throw Exception(result.exception.toString());
    }
    if (result.data == null || result.data!['client_hub_departments'] == null) {
      return [];
    }

    return (result.data!['client_hub_departments'] as List)
        .map((e) => DepartmentModel.fromJson(e))
        .toList();
  }
}
