require_dependency 'redmine_event_outbox/event'
require_dependency 'redmine_event_outbox/hooks/issues_hook'

ActionDispatch::Callbacks.to_prepare do
  require_dependency 'redmine_event_outbox/patches/journal_patch'
  Journal.send(:include, RedmineEventOutbox::Patches::JournalPatch) unless Journal.included_modules.include?(RedmineEventOutbox::Patches::JournalPatch)

  if defined?(Contact)
    require_dependency 'redmine_event_outbox/patches/contact_patch'
    Contact.send(:include, RedmineEventOutbox::Patches::ContactPatch) unless Contact.included_modules.include?(RedmineEventOutbox::Patches::ContactPatch)
  end

  # Optional local integration with the installed RedmineUP helpdesk fork.
  # The outbox plugin stays loadable without helpdesk, but captures first-class
  # helpdesk identity when the plugin is present.
  helpdesk_installed = begin
    Redmine::Plugin.installed?(:redmine_contacts_helpdesk)
  rescue
    false
  end

  if helpdesk_installed
    require_dependency 'helpdesk_ticket'
    require_dependency 'journal_message'

    if defined?(HelpdeskTicket)
      require_dependency 'redmine_event_outbox/patches/helpdesk_ticket_patch'
      HelpdeskTicket.send(:include, RedmineEventOutbox::Patches::HelpdeskTicketPatch) unless HelpdeskTicket.included_modules.include?(RedmineEventOutbox::Patches::HelpdeskTicketPatch)
    end

    if defined?(JournalMessage)
      require_dependency 'redmine_event_outbox/patches/journal_message_patch'
      JournalMessage.send(:include, RedmineEventOutbox::Patches::JournalMessagePatch) unless JournalMessage.included_modules.include?(RedmineEventOutbox::Patches::JournalMessagePatch)
    end
  end
end
