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

class Ticket::AssetsAll
  attr_accessor :user, :ticket

  def initialize(user, ticket)
    @user = user
    @ticket = ticket
  end

  def all_assets(assets = {})
    attributes_to_change = get_attributes_to_change(assets)

    response(compile_assets(attributes_to_change[:assets]), attributes_to_change)
  end

  private

  def compile_assets(assets)
    ticket.assets(assets)

    assets = ApplicationModel::CanAssets.reduce([ticket, articles, mentions].flatten, assets)
    assets = Link.reduce_assets(assets, links)

    if (draft = ticket.shared_draft) && Ticket::SharedDraftZoomPolicy.new(user, draft).show?
      assets = draft.assets(assets)
    end

    add_ai_summary_enabled(assets)

    if Setting.get('checklist') && user.permissions?('ticket.agent')
      ticket.checklist&.assets(assets)

      ticket.referencing_checklists
        .includes(:ticket)
        .each { |elem| elem.assets(assets) }
    end

    assets
  end

  def response(assets, attributes_to_change)
    {
      ticket_id:          ticket.id,
      ticket_article_ids: articles.pluck(:id),
      assets:             assets,
      links:              links,
      tags:               tags,
      mentions:           mentions.pluck(:id),
      time_accountings:   time_accountings,
      form_meta:          attributes_to_change[:form_meta],
    }
  end

  def add_ai_summary_enabled(assets)
    ticket_assets = assets[Ticket.to_app_model]
    return if ticket_assets.blank?

    ticket_asset = ticket_assets[ticket.id]
    return if ticket_asset.blank?

    ticket_assets[ticket.id] = ticket_asset.merge('ai_summary_enabled' => ai_summary_enabled)
  end

  def ai_summary_enabled
    return false if !TicketPolicy.new(user, ticket).agent_read_access?

    Service::Ticket::AIAssistance::SummaryEnabled
      .with_current_user(user)
      .execute(ticket:)
  end

  def get_attributes_to_change(assets)
    Ticket::ScreenOptions.attributes_to_change(
      current_user: user,
      ticket:       ticket,
      screen:       'edit',
      assets:       assets,
    )
  end

  def articles
    @articles ||= ticket.articles.filter { |elem| Ticket::ArticlePolicy.new(user, elem).show? }
  end

  def links
    @links ||= Link.list(
      link_object:       'Ticket',
      link_object_value: ticket.id,
      user:              user,
    )
  end

  def tags
    @tags ||= ticket.tag_list
  end

  def time_accountings
    @time_accountings ||= begin
      if TicketPolicy.new(user, ticket).agent_read_access?
        ticket
          .ticket_time_accounting
          .map { |row| row.slice(:id, :ticket_id, :ticket_article_id, :time_unit, :type_id) }
      else
        []
      end
    end
  end

  def mentions
    @mentions ||= ticket.mentions
  end
end
