class Complaint < ActiveRecord::Base
  attr_accessible :age, :city_id, :content, :educational_level, :email, :employee_name, :firstname, :gender, :institution_id,
    :lastname, :nationality, :occupation_id, :occurred_city_id, :occurred_date, :occurred_place, :position

  # Non database attributes
  attr_accessible :state_id, :occurred_state_id

  # Accessors
  attr_accessor :state_id, :occurred_state_id

  # Associations
  belongs_to :institution
  belongs_to :occurred_city, class_name: 'City'
  belongs_to :city
  belongs_to :occupation

  # Validations
  validates :content,
            :occurred_place,
            :occurred_state_id,
            :occurred_city_id,
            # :firstname,
            # :lastname,
          presence: true

  # validates :email, presence: true, format: { with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }

  # King Tokens
  can_has_tokens :confirm_email, { days_valid: 5 }

  # Acts as TextCaptcha
  acts_as_textcaptcha

  # Enumerated attributes
  #
  enumerated_attribute :educational_level, %w(none primary secondary highschool university postgrade) do
    label none: I18n.t('enum.information_request.educational_level.none')
    label primary: I18n.t('enum.information_request.educational_level.primary')
    label secondary: I18n.t('enum.information_request.educational_level.secondary')
    label highschool: I18n.t('enum.information_request.educational_level.highschool')
    label university: I18n.t('enum.information_request.educational_level.university')
    label postgrade: I18n.t('enum.information_request.educational_level.postgrade')
  end

  enumerated_attribute :nationality, %w(salvadorian foreign) do
    label salvadorian: I18n.t('enum.information_request.nationality.salvadorian')
    label foreign: I18n.t('enum.information_request.nationality.foreign')
  end

  enumerated_attribute :gender, %w(F M) do
    label F: I18n.t('enum.information_request.gender.female')
    label M: I18n.t('enum.information_request.gender.male')
  end

  def deliver_confirmation_email_request
    self.set_confirm_email_token
    UserMailer.complaint_email_confirm(self).deliver
  end

  def deliver_complaint
    UserMailer.new_complaint(self).deliver
  end
end
