require 'minitest/autorun'
require_relative '../../plugins/redmine_contacts_helpdesk/lib/redmine_helpdesk/issue_api_serializer'

Contact = Struct.new(:id, :name, :company, :primary_email, :emails, keyword_init: true)
Ticket = Struct.new(:id, :contact_id, :message_id, :source, :from_address, :to_address, :cc_address, :ticket_date, :customer, keyword_init: true) do
  def is_incoming?
    true
  end
end
Issue = Struct.new(:helpdesk_ticket, keyword_init: true)

class IssueApiSerializerTest < Minitest::Test
  def test_serializes_helpdesk_ticket_with_expanded_contact
    contact = Contact.new(
      :id => 1890,
      :name => 'Callum Mackeonis',
      :company => 'SafeTag Tracking',
      :primary_email => 'callum@safetagtracking.com',
      :emails => ['callum@safetagtracking.com']
    )
    ticket = Ticket.new(
      :id => 35159,
      :contact_id => 1890,
      :message_id => 'message-id',
      :source => 0,
      :from_address => 'callum@safetagtracking.com',
      :to_address => 'contact@ldrprep.com',
      :cc_address => '',
      :ticket_date => Time.utc(2026, 4, 14, 10, 18, 38),
      :customer => contact
    )

    payload = RedmineHelpdesk::IssueApiSerializer.serialize(Issue.new(:helpdesk_ticket => ticket))

    assert_equal 35159, payload[:id]
    assert_equal 1890, payload[:contact_id]
    assert_equal 'callum@safetagtracking.com', payload[:contact][:email]
    assert_equal 'Callum Mackeonis', payload[:contact][:name]
    assert_equal 'SafeTag Tracking', payload[:contact][:company]
    assert_equal '2026-04-14T10:18:38Z', payload[:ticket_date]
  end

  def test_returns_nil_for_non_helpdesk_issue
    assert_nil RedmineHelpdesk::IssueApiSerializer.serialize(Issue.new(:helpdesk_ticket => nil))
  end
end
