class Event < ApplicationRecord
  extend FriendlyId

  friendly_id :name, use: [ :slugged, :finders ]
  acts_as_taggable

  attr_accessor :crop_w, :crop_h, :crop_x, :crop_y, :prod_description

  has_many :proposals,
    ->(proposal) {
      Proposal.with_preloads.where(['proposals.event_id = ?', proposal.id])
    },
    dependent: :destroy do
      def count
        Proposal.
            where(['proposals.event_id = ?', proxy_association.owner.id]).
            count
      end
    end

  has_many :comments, through: :proposals
  has_many :votes, through: :proposals
  belongs_to :user

  validates_date :occurs_at, on: :create, on_or_after: :today

  validates :name, :description, :occurs_at, presence: true
  validates_associated :user
  validates :name, length: { within: 3..150 }
  validates :description, length: { within: 3..400 }

  before_validation :twitter_has_valid_format

  after_update :reprocess_photo, if: :cropping?

  mount_uploader :picture, PictureUploader

  class << self
    def opened
      where(closed_at: nil)
    end

    def most_recent
      order('created_at DESC').limit(3)
    end

    def occurs_first
      order('occurs_at ASC')
    end

    def active
      where('occurs_at >= ?', Time.zone.today)
    end
  end

  def proposals_count
    proposals.size
  end

  def votes_count
    votes.size
  end

  def comments_count
    comments.size
  end

  def closed?
    closed_at.present?
  end

  def close!
    update_attribute(:closed_at, Time.current)
  end

  def reopen!
    update_attribute(:closed_at, nil)
  end

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
  end

  private

  def twitter_has_valid_format
    match = twitter &&
      (twitter.match(/^([a-zA-Z0-9_]{1,15})$/) ||
       twitter.match(/^@([a-zA-Z0-9_]{1,15})$/) ||
       twitter.match(/^(http:\/\/)?www\.twitter\.com\/([a-zA-Z0-9_]{1,15})$/))

    if match
      self.twitter = match[-1]
    elsif twitter.present?
      errors.add(:twitter, :invalid)
    end
  end

  def reprocess_photo
    picture.recreate_versions!
  end
end

