
module JamRuby
  class GenericState < ActiveRecord::Base


    attr_accessible :top_message,  :event_page_top_logo_url, :connection_policy,  as: :admin

    self.table_name = 'generic_state'

    validates :env, :inclusion => {:in => ['development', 'staging', 'production', 'test']}
    validates :connection_policy, json: true

    def self.env
      GenericState.singleton.env
    end

    def self.allow_emails?

      database_environment = singleton.env

      # if the database says we are in production/staging, then the config has to also agree and say we are in production/staging to send emails
      # this is to protect against developers loading a staging or production environment and possibly sending emails to
      # we even go one step further, and make sure ENV['BUILD_NUMBER'] is set, which is something you do in production, but would be very rare in development

      # or if your database says 'development' and config say 'development', then we allow emails to go out too
      (!ENV['BUILD_NUMBER'].nil? && (Environment.mode == 'production' || Environment.mode == 'staging') && (database_environment == 'production' || database_environment == 'staging')) ||
          (database_environment == 'development' && Environment.mode == 'development')
    end

    def self.affiliate_tallied_at
      GenericState.singleton.affiliate_tallied_at
    end

    def self.bounce_check_at
      GenericState.singleton.bounce_check_at
    end

    def self.top_message
      GenericState.singleton.top_message
    end

    def self.event_page_top_logo_url
      GenericState.singleton.event_page_top_logo_url
    end

    def self.connection_policy
      GenericState.connection_policy
    end

    def self.singleton
      GenericState.find('default')
    end

  end
end
