disabled
  F   	ˆl–Ãa¾”ªi?u8 ¸Û71hß�þ\~2¬ã�f1�­F!¿üÿÄ„ tÃ_�uÐb&=LyZËcõÈÇ ?÷     	# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

# What creating a ticket does is covered by spec/services/service/ticket/create_spec.rb and
#   spec/services/service/ticket/article/create_spec.rb â€” this covers the GraphQL surface only:
#   input transformation, authorization, how the service's errors reach the client, and the payload.
RSpec.describe Gql::Mutations::Ticket::Create, :aggregate_failures, type: :graphql do
  let(:query) do
    <<~QUERY
      mutation ticketCreate($input: TicketCreateInput!) {
        ticketCreate(input: $input) {
          ticket {
            id
            title
            group {
              name
            }
            priority {
              name
            }
            customer {
              fullname
            }
            owner {
              fullname
            }
            objectAttributeValues {
              attribute {
                name
              }
              value
            }
            tags
          }
          errors {
            message
            field
          }
        }
      }
    QUERY
  end
  let(:agent)    { create(:agent, groups: [ Group.find_by(name: 'Users')]) }
  let(:customer) { create(:customer) }
  let(:user)     { agent }
  let(:group)    { agent.groups.first }
  let(:priority) { Ticket::Priority.last }

  let(:article_payload) { nil }

  let(:input_base_payload) do
    {
      title:      'Ticket Create Mutation Test',
      groupId:    gql.id(group),
      priorityId: gql.id(priority),
      customer:   { id: gql.id(customer) },
      ownerId:    gql.id(agent),
      tags:       %w[foo bar],
      article:    article_payload
      # pending_time: 10.minutes.from_now,
      # type: ...
    }
  end

  let(:input_payload) { input_base_payload }
  let(:variables)     { { input: input_payload } }

  let(:expected_base_response) do
    {
      'id'                    => gql.id(Ticket.last),
      'title'                 => 'Ticket Create Mutation Test',
      'owner'                 => { 'fullname' => agent.fullname },
      'group'                 => { 'name' => agent.groups.first.name },
      'customer'              => { 'fullname' => customer.fullname },
      'priority'              => { 'name' => Ticket::Priority.last.name },
      'tags'                  => %w[foo bar],
      'objectAttributeValues' => [],
    }
  end

  let(:expected_response) do
    expected_base_response
  end

  def it_creates_ticket(articles: 0, stores: 0)
    expect { gql.execute(query, variables: variables) }
      .to change(Ticket, :count).by(1)
      .and change(Ticket::Article, :count).by(articles)
      .and change(Store, :count).by(stores)
  end

  def it_fails_to_create_ticket
    expect { gql.execute(query, variables: variables) }
      .not_to change(Ticket, :count)
  end

  context 'when creating a new ticket' do

    context 'with an agent', authenticated_as: :agent do

      it 'creates Ticket record' do
        it_creates_ticket
        expect(gql.result.data[:ticket]).to eq(expected_response)
      end

      context 'without title' do
        let(:input_payload) { input_base_payload.tap { |h| h[:title] = '   ' } }

        it 'fails validation' do
          it_fails_to_create_ticket
          expect(gql.result.error_message).to include('Variable $input of type TicketCreateInput! was provided invalid value for title')
        end
      end

      context 'with custom object_attribute', db_strategy: :reset do
        let(:object_attribute) do
          screens = { create: { 'admin.organization': { shown: true, required: false } } }
          create(:object_manager_attribute_text, object_name: 'Ticket', screens: screens).tap do |_oa|
            ObjectManager::Attribute.migration_execute
          end
        end
        let(:input_payload) do
          input_base_payload.merge(
            {
              objectAttributeValues: [ { name: object_attribute.name, value: 'object_attribute_valu