0x00
  E   9ˆl–Ãa¾”ªi?u8 ¸Û71hß�þBóR,ã�f1�¡‘¦™þÜƒpBÛ_�uÐb&=LyZËcõÁÙ M    9# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

# Links two tickets as the ticket view does: the named ticket gets the link and needs change access, the other
#   ticket needs read access, and the kind says what the other ticket is to the named one, in get_ticket's words, so
#   that get_ticket reads the link back as it was asked for. Two tickets are linked once: a second link, in either
#   direction or of another kind, is refused with the kind the link has.
class Service::AI::Tool::LinkTickets < Service::AI::Tool
  OTHER_PREFIX = 'other_'.freeze

  # Written for the model, like every text a tool returns, so not translated.
  # rubocop:disable Zammad/DetectTranslatableString
  KINDS = Service::AI::Tool::GetTicket::LINK_ORDER

  # As a sentence of the result names the link; get_ticket's LINK_WORDS name it from the ticket's side.
  SENTENCE_WORDS = {
    'parent' => 'the parent of',
    'normal' => 'linked to',
    'child'  => 'a child of',
  }.freeze

  description 'Link two tickets of Zammad, as the ticket view does. Name the ticket that gets the link by its ' \
              'ticket_number, ticket_id or ticket_url, and the other ticket by other_ticket_number, other_ticket_id or ' \
              'other_ticket_url. link says what the other ticket is to the named one, as get_ticket names its links: ' \
              "#{KINDS.map { |kind| "#{kind} (#{Service::AI::Tool::GetTicket::LINK_WORDS.fetch(kind)})" }.to_sentence(last_word_connector: ' or ')}. " \
              'Two tickets are linked once; a link is neither changed nor removed here â€” the user does that in Zammad.'
  title 'Link tickets'
  output_schema(
    type:       'object',
    properties: {
      ticket:        Service::AI::Tool::Output::TicketReference::SCHEMA.merge(description: 'The ticket that got the link.'),
      other_ticket:  Service::AI::Tool::Output::TicketReference::SCHEMA.merge(description: 'The ticket it is linked to.'),
      link:          { type: 'string', enum: KINDS, description: 'What the other ticket is to the ticket.' },
      linked_before: { type: 'boolean', description: 'The two were linked already; nothing changed, and link is the kind they have.' },
    },
    required:   %w[ticket other_ticket link linked_before],
  )
  # rubocop:enable Zammad/DetectTranslatableString
  effect      :write
  permission  'ticket.agent'
  offered_in  :mcp
  audiences   :human_agents
  # A link is added, never removed, and a second call links nothing more.
  mcp_annotations destructive_hint: false, open_world_hint: false, idempotent_hint: true

  # As the other writes: the ticket's customer who is no agent of its group passes follow_up?, not this.
  loads :ticket, type: ::Ticket, pundit_method: :agent_update_access?, named: true

  Service::AI::Tool::Locator::Ticket::PARAMETERS.each do |name, spec|
    parameter "#{OTHER_PREFIX}#{name}", **spec, description: "The other ticket: #{spec[:description].sub(%r{\AThe }, 'the ')}"
  end
  parameter :link, type: :string, enum: KINDS, required: true,
                   description: 'What the other ticket is to the named one: parent, normal or child.' # rubocop:disable Zammad/DetectTranslatableString

  def execute
    invalid = invalid_other_ticket
    return Service::AI::Tool::Runtime::Result.error('invalid_arguments', invalid) if invalid

    # The pair's lock, not the ticket's: a call linking the two the other way round locks the same pair first.
    Service::Link::Add.locking_pair(other_ticket, ticket) do
      # Asks nothing, as for a tag or a note, but answers a cancelled or stale request as every write does.
      response = confirmation&.call(required: false, preview:, snapshot: { ticket_id: ticket.id, other_ticket_id: other_ticket.id, link: })
      next response if response

      result(existing: link_or_existing)
    end
  end

  private

  def link
    argument(:link)
  end

  # The other ticket's arguments under the names the ticket locator reads.
  def other_arguments
    @othe