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

FactoryBot.define do
  factory :'ticket/article', aliases: %i[ticket_article] do
    inbound_email

    ticket factory: :ticket, strategy: :create # or else build(:ticket_article).save fails
    from          { 'factory-customer-1@example.com' }
    to            { 'factory-customer-1@example.com' }
    subject       { 'factory article' }

    message_id    { 'factory@id_com_1' }
    body          { 'some message 123' }
    internal      { false }
    sender        { Ticket::Article::Sender.find_by(name: sender_name) }
    type          { Ticket::Article::Type.find_by(name: type_name) }
    updated_by_id { 1 }
    created_by_id { 1 }

    trait :inbound_email do
      transient do
        type_name   { 'email' }
        sender_name { 'Customer' }
      end
    end

    trait :outbound_email do
      transient do
        type_name   { 'email' }
        sender_name { 'Agent' }
      end

      from { ticket.group.name }
      to   { "#{ticket.customer.fullname} <#{ticket.customer.email}>" }
    end

    trait :inbound_phone do
      transient do
        type_name { 'phone' }
        sender_name { 'Customer' }
      end
    end

    trait :outbound_phone do
      transient do
        type_name   { 'phone' }
        sender_name { 'Agent' }
      end

      from { nil }
      to   { ticket.customer.fullname }
    end

    trait :outbound_note do
      transient do
        type_name   { 'note' }
        sender_name { 'Agent' }
      end

      from { ticket.group.name }
    end

    trait :internal_note do
      transient do
        type_name   { 'note' }
        sender_name { 'Agent' }
      end

      from     { ticket.group.name }
      internal { true }
    end

    trait :inbound_web do
      transient do
        type_name   { 'web' }
        sender_name { 'Customer' }
      end
    end

    trait :outbound_web do
      transient do
        type_name   { 'web' }
        sender_name { 'Agent' }
      end
    end

    trait :system_outbound_email do
      transient do
        type_name   { 'email' }
        sender_name { 'System' }
      end
    end

    trait :system_note do
      transient do
        type_name   { 'note' }
        sender_name { 'System' }
      end
    end

    # The flagged article of an error ticket, with the raw email and the processing log attached.
    trait :email_unprocessable do
      system_note

      internal    { true }
      preferences { { email_unprocessable: true } }

      after(:create) do |article|
        create(:store,
               object:      'Ticket::Article',
               o_id:        article.id,
               filename:    'unprocessable_email.eml',
               data:        "From: foo@bar@example.com\nSubject: Foo\n\nLorem ipsum dolor",
               preferences: { 'Content-Type' => 'message/rfc822', 'Mime-Type' => 'message/rfc822', 'email_unprocessable' => 'raw' })

        create(:store,
               object:      'Ticket::Article',
               o_id:        article.id,
               filename:    'processing_log.txt',
               data:        "Failed at: 2026-10-27T10:00:00Z\nException: RuntimeError\nMessage: Lorem ipsum\n",
               preferences: { 'Content-Type' => 'text/plain', 'Mime-Type' => 'text/plain', 'email_unprocessable' => 'log' })
      end
    end

    trait :system_delivery_message do
      transient do
        type_name   { 'note' }
        sender_name { 'System' }
      end

      preferences do
        {
          delivery_message: true,
          notification:     true,
        }
      end
    end

    factory :twitter_article do
      transient do
        type_name   { 'twitter status' }
        sender_name { 'Agent' }
      end

      ticket factory: %i[twitter_ticket]
      subject      { nil }
      body         { Faker::Lorem.sentence }
      content_type { 'text/plain' }
      message_id   { Faker::Number.unique.number(digits: 18) }

      after(:create) do |article, context|
        next if context.sender_name == 'Agent'

        context.ticket.title = article.body

        context.ticket.save!
      end

      trait :inbound do
        transient do
          sender_name  { 'Customer' }
          username     { Faker::X.unique.screen_name }
          sender_id    { Faker::Number.unique.number(digits: 18) }
          recipient_id { Faker::Number.unique.number(digits: 19) }
        end

        from { "@#{username}" }
        to   { "@#{ticket.preferences['channel_screen_name']}" }
        body { "#{to} #{Faker::Lorem.question}" }

        preferences do
          {
            twitter: {
              mention_ids:         [recipient_id],
              geo:                 {},
              retweeted:           false,
              possibly_sensitive:  false,
              in_reply_to_user_id: recipient_id,
              place:               {},
              retweet_count:       0,
              source:              '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>',
              favorited:           false,
              truncated:           false
            },
            links:   [
              {
                url:    "https://twitter.com/_/status/#{sender_id}",
                target: '_blank',
                name:   'on Twitter',
              },
            ],
          }
        end
      end

      trait :outbound do
        transient do
          username     { Faker::X.unique.screen_name }
          sender_id    { Faker::Number.unique.number(digits: 18) }
          recipient_id { Faker::Number.unique.number(digits: 19) }
        end

        from        { "@#{ticket.preferences['channel_screen_name']}" }
        to          { "@#{username}" }
        body        { "#{to} #{Faker::Lorem.sentence}" }
        in_reply_to { Faker::Number.unique.number(digits: 19) }

        preferences do
          {
            twitter: {
              mention_ids:         [recipient_id],
              geo:                 {},
              retweeted:           false,
              possibly_sensitive:  false,
              in_reply_to_user_id: recipient_id,
              place:               {},
              retweet_count:       0,
              source:              '<a href="https://www.canva.com" rel="nofollow">Canva</a>',
              favorited:           false,
              truncated:           false
            },
            links:   [
              {
                url:    "https://twitter.com/_/status/#{sender_id}",
                target: '_blank',
                name:   'on Twitter',
              },
            ],
          }
        end
      end

      trait :reply do
        in_reply_to { Faker::Number.unique.number(digits: 19) }
      end
    end

    factory :twitter_dm_article do
      transient do
        type_name { 'twitter direct-message' }
      end

      ticket factory: %i[twitter_ticket]
      body { Faker::Lorem.sentence }

      trait :pending_delivery do
        transient do
          recipient { association :twitter_authorization }
          sender_id { Faker::Number.unique.number(digits: 10) }
        end

        from         { ticket.owner.fullname }
        to           { recipient.username }
        in_reply_to  { Faker::Number.unique.number(digits: 19) }
        content_type { 'text/plain' }
      end

      trait :delivered do
        pending_delivery
        message_id { Faker::Number.unique.number(digits: 19) }
        preferences do
          {
            delivery_retry:          1,
            twitter:                 {
              recipient_id: recipient.uid,
              sender_id:    sender_id
            },
            links:                   [
              {
                url:    "https://twitter.com/messages/#{recipient.uid}-#{sender_id}",
                target: '_blank',
                name:   'on Twitter'
              }
            ],
            delivery_status_message: nil,
            delivery_status:         'success',
            delivery_status_date:    Time.current
          }
        end
      end
    end

    factory :sms_article do
      inbound

      transient do
        type_name { 'sms' }
      end

      ticket factory: %i[sms_ticket]
      from { Faker::PhoneNumber.cell_phone_in_e164 }
      to   { Faker::PhoneNumber.cell_phone_in_e164 }
      subject { nil }
      body { Faker::Lorem.sentence }
      message_id { Faker::Number.unique.number(digits: 19) }
      content_type { 'text/plain' }

      after(:create) do |article, context|
        next if context.sender_name == 'Agent'

        context.ticket.title = article.body
        context.ticket.preferences.tap do |p|
          p['sms'] = {
            originator: article.from,
            recipient:  article.to,
          }
        end

        context.ticket.save!
      end

      trait :inbound do
        transient do
          sender_name { 'Customer' }
        end

        preferences do
          {
            channel_id: ticket.preferences['channel_id'],
            sms:        {
              reference: message_id,
            },
          }
        end
      end

      trait :outbound do
        transient do
          sender_name { 'Agent' }
        end

        in_reply_to { Faker::Number.unique.number(digits: 19) }

        preferences do
          {
            delivery_retry:          1,
            delivery_status_message: nil,
            delivery_status:         'success',
            delivery_status_date:    Time.current,
          }
        end
      end
    end

    factory :whatsapp_article do
      inbound

      transient do
        type_name          { 'whatsapp message' }
        channel            { Channel.find(ticket.preferences[:channel_id]) }
        from_phone_number  { Faker::PhoneNumber.cell_phone_in_e164 }
        from_name          { Faker::Name.unique.name }
        timestamp_incoming { Time.zone.now.to_i.to_s }
      end

      ticket factory: %i[whatsapp_ticket]
      to { "#{channel.options[:name]} (#{channel.options[:phone_number]})" }
      subject { nil }
      body { Faker::Lorem.sentence }
      content_type { 'text/plain' }

      before(:create) do |_article, context|
        next if context.sender_name == 'Agent' && context.ticket.preferences[:whatsapp].present?

        context.ticket.preferences.tap do |p|
          p['whatsapp'] = {
            from:               {
              phone_number: context.from_phone_number.delete('+'),
              display_name: context.from_name,
            },
            timestamp_incoming: context.timestamp_incoming,
          }
        end
        context.ticket.title = "New WhatsApp message from #{context.from_name} (#{context.from_phone_number})"
        context.ticket.save!
      end

      trait :inbound do
        transient do
          sender_name { 'Customer' }
        end

        message_id { "wamid.#{Faker::Number.unique.number}" }
        from { "#{from_name} (#{from_phone_number})" }
        created_by_id { ticket.customer_id } # NB: influences the value for the from field!

        preferences do
          {
            whatsapp: {
              entry_id:   channel[:options][:phone_number_id],
              message_id: message_id,
            }
          }
        end
      end

      trait :pending_delivery do
        transient do
          sender_name { 'Agent' }
        end

        preferences { {} }

        created_by_id { create(:agent).id } # NB: influences the value for the from field!
        in_reply_to { "wamid.#{Faker::Number.unique.number}" }
      end

      trait :outbound do
        pending_delivery

        message_id { "wamid.#{Faker::Number.unique.number}" }

        preferences do
          {
            delivery_retry:          1,
            whatsapp:                {
              message_id:,
            },
            delivery_status_message: nil,
            delivery_status:         'success',
            delivery_status_date:    Time.current,
          }
        end
      end

      trait :with_attachment_media_document do
        after(:create) do |article, _context|
          create(:store,
                 object:      article.class.name,
                 o_id:        article.id,
                 data:        Faker::Lorem.unique.sentence,
                 filename:    'test.txt',
                 preferences: { 'Content-Type' => 'text/plain' })

          article.preferences.tap do |prefs|
            prefs['whatsapp'] = {
              entry_id:   Faker::Number.unique.number.to_s,
              message_id: "wamid.#{Faker::Number.unique.number}",
              type:       'document',
              media_id:   Faker::Number.unique.number.to_s
            }
          end
          article.save!
        end
      end

      trait :with_media_error do
        after(:create) do |article, _context|
          article.preferences.tap do |prefs|
            prefs['whatsapp'] = {
              entry_id:    Faker::Number.unique.number.to_s,
              message_id:  "wamid.#{Faker::Number.unique.number}",
              type:        'document',
              media_id:    Faker::Number.unique.number.to_s,
              media_error: true,
            }
          end
          article.save!
        end
      end
    end

    factory :telegram_article do
      inbound

      transient do
        type_name { 'telegram personal-message' }
        channel { Channel.find(ticket.preferences[:channel_id]) }
        username { Faker::Internet.username }
      end

      ticket factory: %i[telegram_ticket]
      to { "@#{channel[:options][:bot][:username]}" }
      subject { nil }
      body { Faker::Lorem.sentence }
      message_id { "#{Faker::Number.unique.decimal(l_digits: 1, r_digits: 10)}@telegram" }
      content_type { 'text/plain' }

      after(:create) do |article, context|
        next if context.sender_name == 'Agent'

        context.ticket.title = article.body
        context.ticket.preferences.tap do |p|
          p['telegram'] = {
            bid:     context.channel[:options][:bot][:id],
            chat_id: (article.preferences[:telegram] && article.preferences[:telegram][:chat_id]) || Faker::Number.unique.number(digits: 10),
          }
        end

        context.ticket.save!
      end

      trait :inbound do
        transient do
          sender_name { 'Customer' }
        end

        created_by_id { ticket.customer_id } # NB: influences the value for the from field!

        preferences do
          {
            message:   {
              created_at: Time.current.to_i,
              message_id: message_id,
              from:       ActionController::Parameters.new(
                'id'            => Faker::Number.unique.number,
                'is_bot'        => false,
                'first_name'    => Faker::Name.unique.first_name,
                'last_name'     => Faker::Name.unique.last_name,
                'username'      => username,
                'language_code' => 'en',
              ),
            },
            update_id: Faker::Number.unique.number(digits: 8),
          }
        end
      end

      trait :outbound do
        transient do
          sender_name { 'Agent' }
        end

        to { "@#{username}" }
        created_by_id { create(:agent).id } # NB: influences the value for the from field!
        in_reply_to { "#{Faker::Number.unique.decimal(l_digits: 1, r_digits: 10)}@telegram" }

        preferences do
          {
            delivery_retry:          1,
            telegram:                {
              date:       Time.current.to_i,
              from_id:    Faker::Number.unique.number(digits: 10),
              chat_id:    Faker::Number.unique.number(digits: 10),
              message_id: Faker::Number.unique.number,
            },
            delivery_status_message: nil,
            delivery_status:         'success',
            delivery_status_date:    Time.current,
          }
        end
      end
    end

    factory :facebook_article do
      inbound

      transient do
        channel { Channel.find(ticket.preferences[:channel_id]) }
        post_id { Faker::Number.unique.number(digits: 15) }
        permalink_url { "https://www.facebook.com/#{channel[:options][:pages][0][:id]}/posts/#{post_id}/?comment_id=#{post_id}" }
      end

      ticket factory: %i[facebook_ticket]
      subject { nil }
      body { Faker::Lorem.sentence }
      message_id { "#{Faker::Number.unique.number(digits: 16)}_#{Faker::Number.unique.number(digits: 15)}" }
      content_type { 'text/plain' }

      after(:create) do |article, context|
        next if context.sender_name == 'Agent'

        context.ticket.title = article.body
        context.ticket.preferences.tap do |p|
          p['channel_fb_object_id'] = context.post_id,
                                      p['facebook'] = {
                                        permalink_url: context.permalink_url,
                                      }
        end

        context.ticket.save!
      end

      trait :inbound do
        transient do
          type_name { 'facebook feed post' }
          sender_name { 'Customer' }
        end

        from { ticket.customer.fullname }
        to { channel[:options][:pages][0][:name] }

        preferences do
          {
            links: [
              {
                url:    permalink_url,
                target: '_blank',
                name:   'on Facebook',
              },
            ],
          }
        end
      end

      trait :outbound do
        transient do
          type_name { 'facebook feed comment' }
          sender_name { 'Agent' }
        end

        from { channel[:options][:pages][0][:name] }
        to { ticket.customer.fullname }
        in_reply_to { "#{Faker::Number.unique.number(digits: 16)}_#{Faker::Number.unique.number(digits: 15)}" }

        preferences do
          {
            delivery_retry:          1,
            delivery_status_message: nil,
            delivery_status:         'success',
            delivery_status_date:    Time.current,
          }
        end
      end
    end

    trait :with_attachment do
      transient do
        attachment_filename { 'hello_world.txt' }
        attachment_path     { 'spec/fixtures/files/upload' }
        attachment          { File.open(File.join(attachment_path, attachment_filename)) }
      end

      after(:create) do |article, context|
        create(:store,
               object:      article.class.name,
               o_id:        article.id,
               data:        context.attachment.read,
               filename:    File.basename(context.attachment.path),
               preferences: {})
      end
    end

    trait :with_inline_attachment do
      content_type { 'text/html' }

      after(:build) do |article|
        article.body += ' inline img: <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=">'
      end
    end

    trait :with_prepended_attachment do
      transient do
        attachment            { File.open('spec/fixtures/files/upload/hello_world.txt') }
        override_content_type { nil }
        attachments_count     { 1 }
      end

      after(:build) do |article, context|
        filename     = File.basename(context.attachment.path)
        content_type = context.override_content_type || MIME::Types.type_for(filename).first&.content_type

        attachments = []

        context.attachments_count.times do
          attachments << create(:store,
                                data:        context.attachment.read,
                                filename:    filename,
                                preferences: { 'Content-Type' => content_type })
        end

        article.attachments = attachments
      end
    end

  end
end
