import graphene from django.contrib.auth.models import User from graphene import Field, Boolean, List, String from backend.core.commands import ( CreateProfileCommand, UpdateProfileCommand, DeleteProfileCommand, SearchProfilesCommand ) from backend.core.repositories import ProfileRepository from backend.graphql_api.types import ProfileType from backend.graphql_api.inputs import ProfileCreateInput, ProfileUpdateInput class CreateProfileMutation(graphene.Mutation): """Mutation to create a new profile.""" class Arguments: input = ProfileCreateInput(required=True) profile = Field(ProfileType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: try: user = User.objects.get(id=input.user_id) except User.DoesNotExist: return { 'profile': None, 'success': False, 'errors': ['User not found'], 'message': "User with the specified ID does not exist" } profile_repo = ProfileRepository() command = CreateProfileCommand( profile_repo=profile_repo, user=user, first_name=input.first_name, last_name=input.last_name, primary_phone=input.primary_phone, email=input.email, role=input.role, secondary_phone=input.secondary_phone ) result = command.execute() return { 'profile': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'profile': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while creating the profile" } class UpdateProfileMutation(graphene.Mutation): """Mutation to update an existing profile.""" class Arguments: input = ProfileUpdateInput(required=True) profile = Field(ProfileType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: profile_repo = ProfileRepository() command = UpdateProfileCommand( profile_repo=profile_repo, profile_id=input.id, first_name=input.first_name, last_name=input.last_name, primary_phone=input.primary_phone, email=input.email, role=input.role, secondary_phone=input.secondary_phone ) result = command.execute() return { 'profile': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'profile': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while updating the profile" } class DeleteProfileMutation(graphene.Mutation): """Mutation to delete a profile.""" class Arguments: id = graphene.ID(required=True) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id): try: profile_repo = ProfileRepository() command = DeleteProfileCommand( profile_repo=profile_repo, profile_id=id ) result = command.execute() return { 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'success': False, 'errors': [str(e)], 'message': "An error occurred while deleting the profile" } class SearchProfilesMutation(graphene.Mutation): """Mutation to search profiles.""" class Arguments: search_term = graphene.String(required=True) role = graphene.String() profiles = List(ProfileType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, search_term, role=None): try: profile_repo = ProfileRepository() command = SearchProfilesCommand( profile_repo=profile_repo, search_term=search_term, role=role ) result = command.execute() return { 'profiles': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'profiles': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while searching profiles" }