
class Need < ActiveRecord::Base
  include Workflow

  
  belongs_to :referent_technique, :class_name => "Customer"
  belongs_to :referent_negos, :class_name => "Customer"


  has_many :domains, :through => :domain_needs
  has_many :domain_needs, :dependent => :destroy

  scope :shared, -> {
    where(state: ["verified", "negociating", "negociated", "failed"])
  }




  scope :with_wishes_count, -> { joins('left join wishes on wishes.need_id = needs.id')
    .select('needs.*, count(wishes.id) as wishes_count')
    .group("needs.id")
  }

  scope :with_messages_count, -> { joins('left join messages on messages.need_id = needs.id')
    .select('needs.*, count(messages.id) as messages_count')
    .group("needs.id")
  }

  scope :search, -> (search) {
    where('needs.title LIKE ? OR needs.description LIKE ?', "%#{search}%", "%#{search}%")
  }

  scope :domain_in, -> (domain_ids) {
    joins(:domains).where('domains.id IN(?)', domain_ids)
  }



  after_create :create


  workflow_column :state
  acts_as_paranoid

  has_many :wishes, dependent: :destroy
  has_many :customers, -> { uniq }, through: :wishes
  has_many :messages, dependent: :destroy
  has_many :offers, dependent: :destroy
  belongs_to :image_file
  belongs_to :category, class_name: "NeedCategory"

  validates :title, :presence => true, length: {within: 4..128}
  validates :description, presence: true, length: {maximum: 65535}
  belongs_to :author, class_name: 'Customer'

  mount_uploader :devis, DevisUploader

  # Need's workflow lifecycle
  workflow do
    state :created do
      event :validate, :transitions_to => :verified
      event :refuse, :transitions_to => :refused
    end
    state :refused
    state :verified do
      event :negociate, :transitions_to => :negociating
    end
    state :negociating do
      event :back_to_verified, :transitions_to => :verified
      event :accept, :transitions_to => :negociated
      event :reject, :transitions_to => :failed
    end
    state :negociated do
      event :back_to_negociating, :transitions_to => :negociating
    end
    state :failed
  end

  def create
    if self.author
      # Find all admins with email
      admins = Admin.where.not(email: nil)
      admins.each do |admin|
        AdminMailer.new_need(admin, self).deliver
      end
    end
  end

  def validate
    if self.author
      CustomerMailer.validate_need(self).deliver
    end
  end

  def negociate
    customers = self.customers
    customers.each do |customer|
      CustomerMailer.negociate_need(self, customer).deliver
    end
  end

  def accept
    customers = self.customers
    customers.each do |customer|
      CustomerMailer.need_negociated(customer, self).deliver
    end
  end

  def reject
    customers = self.customers
    customers.each do |customer|
      CustomerMailer.need_negociation_failed(customer, self).deliver
    end
  end

  def refuse
    if self.author
      CustomerMailer.refuse_need(self).deliver
    end
  end

  def back_to_verified

  end

  def back_to_negociating

  end




  # Human state conversion
  def human_state
    case state
    when 'created'
      "En attente de validation"
    when 'verified'
      "En sondage"
    when 'refused'
      "Refusé"
    when 'negociating'
      "En négociation"
    when 'negociated'
      "Négociation terminée"
    when 'failed'
      "Négociation échouée"
    else
      "Inconnu"
    end
  end

  def categories
    collection = []
    parent_category = self.category
    if(parent_category)
      collection << parent_category
      parent_category.ancestors.each do |c|
        collection << c
      end

    end
    collection
  end

  def category_path
    if !@category_path
      @category_path = self.categories.map { |c| c.name }.reverse.join(' \ ')
    end
    @category_path
  end


end
