class Admin::ConversationsController < ApplicationController
  layout "admin"
  before_filter :auth_admin


  def index

    @conversations = ContactMessage.select([
      "count(CASE WHEN contact_messages.read_by_admin = FALSE THEN 1 END ) as unread_count",
      "max(created_at) as last_at",
      "count(contact_id) as messages_count",
      "contact_messages.*"])
      .group(:contact_id)
      .order(created_at: :desc)
      .page(params[:page])


  end

  def show
    @customer = Customer.find(params[:id])
    ContactMessage.where(contact_id: params[:id]).update_all(read_by_admin: true)
    @contact_messages = ContactMessage.where('customer_id = ? OR contact_id = ?', params[:id], params[:id])
      .order(created_at: :desc).page params[:page]

  end


end
