import graphene from graphene import Field, Boolean, List, String, Float from backend.core.commands import ( CreateLaborCommand, UpdateLaborCommand, DeleteLaborCommand, EndLaborCommand, CalculateLaborCostCommand ) from backend.core.repositories import LaborRepository, AccountRepository from backend.graphql_api.types import LaborType from backend.graphql_api.inputs import LaborCreateInput, LaborUpdateInput class CreateLaborMutation(graphene.Mutation): """Mutation to create a new labor record.""" class Arguments: input = LaborCreateInput(required=True) labor = Field(LaborType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: labor_repo = LaborRepository() account_repo = AccountRepository() command = CreateLaborCommand( labor_repo=labor_repo, account_repo=account_repo, account_id=input.account_id, amount=input.amount, start_date=input.start_date, end_date=input.end_date ) result = command.execute() return { 'labor': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'labor': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while creating the labor record" } class UpdateLaborMutation(graphene.Mutation): """Mutation to update an existing labor record.""" class Arguments: input = LaborUpdateInput(required=True) labor = Field(LaborType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: labor_repo = LaborRepository() account_repo = AccountRepository() command = UpdateLaborCommand( labor_repo=labor_repo, account_repo=account_repo, labor_id=input.id, amount=input.amount, start_date=input.start_date, end_date=input.end_date ) result = command.execute() return { 'labor': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'labor': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while updating the labor record" } class DeleteLaborMutation(graphene.Mutation): """Mutation to delete a labor record.""" class Arguments: id = graphene.ID(required=True) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id): try: labor_repo = LaborRepository() command = DeleteLaborCommand( labor_repo=labor_repo, labor_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 labor record" } class EndLaborMutation(graphene.Mutation): """Mutation to end a labor record.""" class Arguments: id = graphene.ID(required=True) end_date = graphene.String() labor = Field(LaborType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id, end_date=None): try: labor_repo = LaborRepository() command = EndLaborCommand( labor_repo=labor_repo, labor_id=id, end_date=end_date ) result = command.execute() return { 'labor': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'labor': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while ending the labor record" } class CalculateLaborCostMutation(graphene.Mutation): """Mutation to calculate labor cost.""" class Arguments: account_id = graphene.ID() start_date = graphene.String() end_date = graphene.String() total_cost = Float() success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, account_id=None, start_date=None, end_date=None): try: labor_repo = LaborRepository() account_repo = AccountRepository() command = CalculateLaborCostCommand( labor_repo=labor_repo, account_repo=account_repo, account_id=account_id, start_date=start_date, end_date=end_date ) result = command.execute() return { 'total_cost': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'total_cost': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while calculating labor cost" }