module Users
  class ListsController < Users::ApplicationController

    def index
      #lists = current_user.lists.include_relation(:supplier, :table)
      lists = List.for_user(current_user, page: params[:page], per_page: params[:per_page].presence || 25)
      #lists.include_relation(:supplier)
      #lists.reject!{|l| l.id == params[:exclude_list]} if params[:exclude_list].present?
      #lists.reject!{|l| l.id == current_user.active_list_id } if current_user && current_user.active_list_id.present? # see spec Loading lists and switching to the order products view works, lists loading may unlink active list orders
      lists.include_relation(:users, :supplier)
      render json: lists, include: %w[supplier users], meta: {total_pages: lists.total_pages, page: lists.current_page} #, root: :lists
    end

    #EMBER
    def current
      @list = current_user.active_list
      params[:id] = @list.try(:id) # serializer determines collection or not based on the presence of this
      show
    end

    def table
      list = List.find(params[:id])
      render json: {}, status: :not_found and return unless list.present?
      @table = list.table
      render json: @table
    end

    def show
      @list ||= List.find(params[:id]) if params[:id]
      render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id)
      render json: @list, include: %w[supplier users join_requests join_requests.user]
    end

    # POST /user/list_needs_payment.json
    def needs_payment
      @list = active_list
      render json: json_alert('messages.no_active_list', list_active: false) and return unless @list.try(:id).to_s == params[:id]
      @list.needs_payment!
      render json: @list
    end

    # POST /user/lists/:id/move_table.json?table_id=....
    # used to move the table
    # TODO wrap logic of actions
    # - table_info
    # - move_table
    # into separate class and implement security in a non stupid way as it is now
    def move_to_table
      render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
      render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
      @table = Table.find(params[:table_id])
      if @table.occupied?
        render json: {occupied: true}
      elsif @table.reserved?
        render json: {reserved: true}
      elsif active_list.supplier_id != @table.supplier_id
        res[:other_supplier] = true if active_list.supplier_id != @table.supplier_id
        res[:current_table_id] = active_list.table_id
      else
        active_list.move_to_table! @table
        render json: {occupied: false, reserved: false}
      end
    end

    # Used by the user Ember app
    # please also look and mirror this action in the tables controller
    # POST /user/lists/:id/order_products
    def order_products
      res = {}
      unless active_list.try(:active?)
        res[:list_closed] = true
        render json: res, status: 404
        return
      end
      res[:supplier_closed] = active_list.supplier.closed?
      unless res[:supplier_closed]
        # Create new list
        active_list.place_order product_orders: new_order_product_orders, user: current_user, first_order: false
        # do not add payload, since relevant data is added through message bus (Faye)
        #res[:payload] = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[product_orders])
      end
      render json: res
    end

    # POST /user/reject_join_request?user_id=1
    def reject_join_request
      render js: '' and return unless params[:user_id].present?
      active_list && active_list.reject_join_request_for_user!(params[:user_id])
      head :no_content
    end

    # POST /user/approve_join_request?user_id=1
    def approve_join_request
      render js: '' and return unless params[:user_id].present?
      @user = User.find(params[:user_id])
      active_list && active_list.approve_join_request_for_user!(@user)
      head :no_content
    end

  end
end
