import graphene from graphene import Field, Boolean, List, String, JSONString from backend.core.commands import ( CreateReportCommand, UpdateReportCommand, DeleteReportCommand, GetTeamMemberReportsCommand, GetTeamMemberActivityCommand, GetTeamSummaryCommand ) from backend.graphql_api.types import ReportType from backend.graphql_api.inputs import ReportCreateInput, ReportUpdateInput class CreateReportMutation(graphene.Mutation): """Mutation to create a new report.""" class Arguments: input = ReportCreateInput(required=True) report = Field(ReportType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: command = CreateReportCommand( team_member_id=input.team_member_id, report_date=input.report_date, service_ids=input.service_ids, project_ids=input.project_ids, notes=input.notes ) result = command.execute() return { 'report': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'report': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while creating the report" } class UpdateReportMutation(graphene.Mutation): """Mutation to update an existing report.""" class Arguments: input = ReportUpdateInput(required=True) report = Field(ReportType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: command = UpdateReportCommand( report_id=input.id, team_member_id=input.team_member_id, report_date=input.report_date, service_ids=input.service_ids, project_ids=input.project_ids, notes=input.notes ) result = command.execute() return { 'report': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'report': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while updating the report" } class DeleteReportMutation(graphene.Mutation): """Mutation to delete a report.""" class Arguments: id = graphene.ID(required=True) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id): try: command = DeleteReportCommand(report_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 report" } class GetTeamMemberActivityMutation(graphene.Mutation): """Mutation to get activity summary for a team member.""" class Arguments: team_member_id = graphene.ID(required=True) start_date = graphene.String() end_date = graphene.String() activity = JSONString() success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, team_member_id, start_date=None, end_date=None): try: command = GetTeamMemberActivityCommand( team_member_id=team_member_id, start_date=start_date, end_date=end_date ) result = command.execute() return { 'activity': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'activity': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while retrieving team member activity" } class GetTeamSummaryMutation(graphene.Mutation): """Mutation to get activity summary for all team members.""" class Arguments: start_date = graphene.String() end_date = graphene.String() summary = JSONString() success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, start_date=None, end_date=None): try: command = GetTeamSummaryCommand( start_date=start_date, end_date=end_date ) result = command.execute() return { 'summary': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'summary': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while retrieving team summary" } class GetTeamMemberReportsMutation(graphene.Mutation): """Mutation to get reports for a team member.""" class Arguments: team_member_id = graphene.ID(required=True) start_date = graphene.String() end_date = graphene.String() reports = List(ReportType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, team_member_id, start_date=None, end_date=None): try: command = GetTeamMemberReportsCommand( team_member_id=team_member_id, start_date=start_date, end_date=end_date ) result = command.execute() return { 'reports': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'reports': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while retrieving team member reports" }