module Users
  class TablesController < Users::ApplicationController
    respond_to :json
    def show
      @table = Table.find(params[:id])
      render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
    end

    def supplier
      table = Table.find(params[:id])
      supplier = table.supplier
      supplier.product_categories.include_relations(products: :product_variants)
      render json: supplier, include: %w[
        product_categories
        product_categories.products
        product_categories.products.product_variants
      ]
    end

    # POST /tables/:id/needs_help.json
    def needs_help
      #@table = Table.find(params[:id])
      render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
      active_list.needs_help!
      #render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
      head :no_content
    end

    # POST /user/tables/:id/join
    def join
      render json: json_alert('messages.table_not_found') and return unless params[:id].present?
      @table = Table.find(params[:id])
      if @list = @table.active_list
        @list.send_table_join_request_for_user! current_user
      end
      head :no_content
    end

    # GET /user/table_info.json
    # used for moving table request
    # 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 status_info
      render json: json_alert('messages.table_not_found') and return unless params[:id].present?
      table = Table.find(params[:id])
      res = {}
      res[:occupied] = table.occupied?
      res[:reserved] = table.reserved?
      res[:supplier_closed] = table.supplier.closed?
      res[:table] = table.attributes
      if active_list.present?
        res[:other_supplier] = true if active_list.supplier_id != table.supplier_id
        res[:current_table_id] = active_list.table_id
        res[:current_list_id] = active_list.id
      end
      render json: res
    end

    # Used by the user Ember app
    # NOTE: ordering on a table always creates a new list or failes if the conditions for creating a new
    # list are not met. To order on an already open list send the request to the lists controller
    # POST /user/tables/:id/order_products
    def order_products
      table = Table.find(params[:id])
      res = {}
      res[:active_list_present] = true if active_list.present?
      res[:occupied] = table.occupied?
      res[:reserved] = table.reserved?
      res[:supplier_closed] = table.supplier.closed?
      res[:no_product_orders] = true unless product_orders = new_order_product_orders.presence
      unless res[:occupied] or res[:supplier_closed] or res[:no_product_orders] or res[:active_list_present]
        # Create new list
        list = List.from_table( table, current_user )
        res[:active_list_id] = list.id # used to set the active list in the app
        order = list.place_order product_orders: product_orders, user: current_user, first_order: true
        res[:supplier_orders_placed_count] = list.supplier.orders_placed_count
        res[:payload] = Users::OrderSerializer.serialize(order, include: %w[list list.users product_orders])
      end
      render json: res
    end
  end
end
