require 'minitest/autorun'

RAILS_ENV = ENV["RAILS_ENV"] = "test"

$:.unshift File.dirname(__FILE__) + '/../lib'
require 'mail_form'

require 'active_support'
require 'active_support/test_case'
require 'active_support/string_inquirer'
require 'action_controller'
require 'action_controller/test_case'
require 'action_mailer'

if ActiveSupport::TestCase.respond_to?(:test_order=)
  ActiveSupport::TestCase.test_order = :random
end

ActionMailer::Base.delivery_method = :test
I18n.enforce_available_locales = false

module Rails
  def self.env
    @env ||= ActiveSupport::StringInquirer.new('test')
  end
end

class ContactForm < MailForm::Base
  attribute :name,     validate: true
  attribute :email,    validate: /\A[^@\s]+@[^@\s]+\z/i
  attribute :category, validate: ["Interface bug", "General"], allow_blank: true
  attribute :nickname, captcha: true

  attributes :created_at, :message, validate: :callback

  def headers
    { to: 'my.email@my.domain.com' }
  end

  def initialize(*)
    super
    @_callback_run = false
  end

  def callback
    @_callback_run = true
  end

  def callback_run?
    @_callback_run
  end
end

class AdvancedForm < ContactForm
  append :remote_ip, :user_agent, :session

  def headers
    { to: [ 'my.first@email.com', 'my.second@email.com' ],
      subject: "My Advanced Form",
      from: %{"#{name}" <#{email}>},
      "return-path" => "mypath"
    }
  end
end

class FileForm < ContactForm
  attribute :file, attachment: true, validate: true

  def headers
    to = if file
      "contact_file@my.domain.com"
    else
      "contact@my.domain.com"
    end
    { to: to }
  end
end

class ActiveRecordForm
  include ActiveModel::Model

  # Simulate Active Record `*_create` callbacks.
  extend ActiveModel::Callbacks
  define_model_callbacks :create

  def save
    if valid?
      run_callbacks(:create) { true }
    else
      false
    end
  end

  include MailForm::Delivery

  attribute :name, validate: true
  attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
  attribute :nickname, captcha: true

  def headers
    {
      to: "your.email@your.domain.com",
      subject: "User created an account"
    }
  end
end
