class InstitutionConfidentialInfo < ActiveRecord::Base
  attr_accessible :declaration, :category, :admin_unit, :classification_date, :classification_type, 
                  :legal_foundation, :justification, :declassification_date, :file, :institution_id, :status
  
  # Associations
  belongs_to :institution
  has_many :institution_historical_confidential_infos, dependent: :destroy

  # Validations
  validates :declaration, :category, :admin_unit, :classification_date, :classification_type, :declassification_date,
            :institution_id, presence: true

  accepts_nested_attributes_for :institution_historical_confidential_infos, allow_destroy: true

  enumerated_attribute :status, %w(created extension declassified) do
    label created: "Creado"
    label extension: "Prorroga"
    label declassified: "Desclasificado"
  end

  after_create :verify_status, if: Proc.new{|o| o.status.blank?}

  def data_for_csv
    [
      declaration, 
      category, 
      admin_unit, 
      status, 
      classification_date, 
      classification_type, 
      legal_foundation, 
      justification, 
      declassification_date
    ]
  end

  def self.header_for_csv
    [:declaration, :category, :admin_unit, :status, :classification_date, :classification_type, :legal_foundation, :justification, :declassification_date]
  end

  def self.attributes_for_grid
    [
      :institution_id,
      :declaration,
      :category,
      :admin_unit,
      :classification_date,
      :classification_type,
      :legal_foundation,
      :justification,
      :declassification_date
    ]
  end

  def self.per_page
    30
  end

  def verify_status
    if self.status.blank?
      self.status = "created"
      self.save!
    end
  end

end
