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

module Gql::Types
  class TicketType < BaseObject
    include Gql::Types::Concerns::HasDefaultModelFields
    include Gql::Types::Concerns::HasScopedModelUserRelations
    include Gql::Types::Concerns::HasInternalIdField
    include Gql::Types::Concerns::HasInternalNoteField
    include Gql::Types::Concerns::HasPunditAuthorization

    description 'Tickets'

    # Even though we might fetch tickets with 'overview' permissions in the first place,
    #   check that they always have 'read' permissions as well, as that is logicaly
    #   included in 'overview'.
    def self.scope_items(items, ctx)
      # special handling for GraphQL fragment cache value
      return items if items.is_a?(GraphQL::Execution::Interpreter::RawValue)

      # special handling for Array values - scoping was done before
      return items if items.is_a?(Array)

      TicketPolicy::ReadScope.new(ctx.current_user, items).resolve
    end

    implements Gql::Types::ObjectAttributeValuesInterface
    implements Gql::Types::TagsInterface

    belongs_to :group, Gql::Types::GroupType, null: false
    belongs_to :priority, Gql::Types::Ticket::PriorityType, null: false
    belongs_to :state, Gql::Types::Ticket::StateType, null: false
    belongs_to :organization, Gql::Types::OrganizationType
    belongs_to :owner, Gql::Types::UserType, null: false
    belongs_to :customer, Gql::Types::UserType, null: false
    belongs_to :create_article_type, Gql::Types::Ticket::Article::TypeType

    # Don't expose articles at this point as they can only be used safely with agent read permission.
    # field :articles, Gql::Types::Ticket::ArticleType.connection_type, null: false

    field :policy, Gql::Types::Policy::TicketType, null: false, method: :itself

    field :number, String, null: false
    field :title, String, null: false

    field :first_response_at, GraphQL::Types::ISO8601DateTime
    field :first_response_escalation_at, GraphQL::Types::ISO8601DateTime
    field :first_response_in_min, Integer
    field :first_response_diff_in_min, Integer
    field :close_at, GraphQL::Types::ISO8601DateTime
    field :last_close_at, GraphQL::Types::ISO8601DateTime
    field :close_escalation_at, GraphQL::Types::ISO8601DateTime
    field :close_in_min, Integer
    field :close_diff_in_min, Integer
    field :update_escalation_at, GraphQL::Types::ISO8601DateTime
    field :update_in_min, Integer
    field :update_diff_in_min, Integer
    field :last_contact_at, GraphQL::Types::ISO8601DateTime
    field :last_contact_agent_at, GraphQL::Types::ISO8601DateTime
    field :last_contact_customer_at, GraphQL::Types::ISO8601DateTime
    field :last_owner_update_at, GraphQL::Types::ISO8601DateTime
    field :escalation_at, GraphQL::Types::ISO8601DateTime
    field :pending_time, GraphQL::Types::ISO8601DateTime
    field :initial_channel, Gql::Types::Enum::Channel::AreaType, description: 'The initial channel of the ticket.'

    # field :create_article_sender_id, Integer
    field :article_count, Integer, description: "Count of ticket articles that were not sent by 'System'."
    # field :type, String
    field :preferences, GraphQL::Types::JSON

    field :state_color_code, Gql::Types::Enum::TicketStateColorCodeType, null: false, description: 'Ticket color indicator state.'

    scoped_fields do
      field :time_unit, Float
      field :time_units_per_type, [Gql::Types::Ticket::TimeAccountingTypeSumType]
      field :shared_draft_zoom_id, GraphQL::Types::ID, description: 'The Shared draft ID if the ticket has a shared draft.'
      field :checklist, Gql::Types::ChecklistType, description: 'Returns the checklist of this ticket, if present'
      field :referencing_checklist_tickets, [Gql::Types::TicketType, { null: false }], description: 'Returns (only accessible) other tickets which reference the current ticket'
      field :external_references, Gql::Types::TicketExternalReferencesType, null: true, description: 'Returns links to external services'
      field :ai_agent_running, Boolean, description: 'Returns true if an AI agent is running for this ticket'
      field :ai_summary_enabled, Boolean, description: 'Returns true if the AI summary is enabled for this ticket'
      field :ai_stored_summary, Gql::Types::Ticket::AIAssistance::SummaryType, description: 'Returns the stored and current AI summary in the locale of the current user, never triggers a generation'
      field :first_article, Gql::Types::Ticket::ArticleType, description: 'First article of this ticket, excluding system notifications'
      field :last_article, Gql::Types::Ticket::ArticleType, description: 'Last article of this ticket, excluding system notifications'
    end

    internal_fields do
      field :subscribed, Boolean
      field :mentions, Gql::Types::MentionType.connection_type
    end

    def initial_channel
      return nil if !@object.preferences['channel_area']

      @object.preferences['channel_area']
    end

    def subscribed
      ::Mention.subscribed?(@object, context.current_user)
    end

    def state_color_code
      Service::Ticket::StateColorCode.execute(ticket: @object)
    end

    def time_units_per_type
      time_units_per_type = time_units_per_type_data
      return [] if time_units_per_type.empty?

      if time_units_per_type.length.eql?(1) && time_units_per_type[0][:name].eql?(__('None'))
        []
      else
        time_units_per_type
      end
    end

    def shared_draft_zoom_id
      return nil if !@object.shared_draft

      Gql::ZammadSchema.id_from_object(@object.shared_draft)
    end

    def checklist
      return nil if !Setting.get('checklist')

      @object.checklist
    end

    def referencing_checklist_tickets
      return nil if !Setting.get('checklist')

      ::Checklist.tickets_referencing(@object, context.current_user)
    end

    def external_references
      Service::Ticket::ExternalReferences::List.execute(ticket: @object)
    end

    def ai_summary_enabled
      Service::Ticket::AIAssistance::SummaryEnabled
        .with_current_user(context.current_user)
        .execute(ticket: @object)
    end

    def ai_stored_summary
      Service::Ticket::AIAssistance::StoredSummary
        .with_current_user(context.current_user)
        .execute(ticket: @object)
    end

    # The same article set the summary reads: internal notes included, because
    #   these fields are only available to agents.
    def first_article
      @object.articles.without_system_notifications.first
    end

    def last_article
      @object.articles.without_system_notifications.last
    end

    private

    def time_units_per_type_data
      return [] if !@object.ticket_time_accounting.exists?(['type_id IS NOT NULL'])

      @object.ticket_time_accounting
        .group_by { |r| r[:type_id] }
        .map { |type_id, entries| time_accounting_type_sum(type_id, entries) }
        .flatten
        .sort_by { |r| r[:time_unit] }
        .reverse
    end

    def time_accounting_type_sum(type_id, entries)
      [
        { name:      time_accounting_types[type_id] || __('None'),
          time_unit: entries.inject(0) { |sum, entry| sum + entry.time_unit } },
      ]
    end

    def time_accounting_types
      @time_accounting_types ||= ::Ticket::TimeAccounting::Type.all.to_h { |type| [type.id, type.name] }
    end
  end
end
