# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

module Gql::Mutations
  class Ticket::Update < BaseMutation
    include Gql::Mutations::Ticket::Concerns::HandlesGroup

    description 'Update a ticket.'

    argument :ticket_id, GraphQL::Types::ID, loads: Gql::Types::TicketType, loads_pundit_method: :follow_up?, description: 'The ticket to be updated'
    argument :input, Gql::Types::Input::Ticket::UpdateInputType, description: 'The ticket data'
    argument :meta, Gql::Types::Input::Ticket::UpdateMetaInputType, required: false, description: 'The ticket metadata'

    field :ticket, Gql::Types::TicketType, description: 'The updated ticket. If this is present but empty, the mutation was successful but the user has no rights to view the updated ticket.'

    requires_permission 'ticket.agent', 'ticket.customer'

    def resolve(ticket:, input:, meta: nil)
      Gql::Types::Input::Ticket::UpdateInputType.merge_object_attribute_values!(input)

      Gql::Types::Input::Ticket::UpdateInputType.sanitize_agent_only_fields!(
        input.to_h,
        user:     context.current_user,
        group_id: ticket.group_id
      )

      return group_has_no_email_error if !group_has_email?(input: input)

      {
        ticket: Service::Ticket::Update
          .with_current_user(context.current_user)
          .execute(ticket: ticket, ticket_data: input, skip_validators: meta&.dig(:skip_validators), macro: meta&.dig(:macro))
      }
    rescue Exceptions::InvalidAttribute => e
      field = e.attribute == 'email_recipient' ? 'article.to' : e.attribute

      error_response({ field:, message: e.message })
    rescue => e
      raise e if !e.class.name.starts_with?('Service::Ticket::Update::Validator')

      error_response(
        message:   e.message,
        exception: e.class,
      )
    end
  end
end
