module Suppliers
  class SuppliersController < Suppliers::ApplicationController
    def index
      render json: {}

    end

    def show
      current_supplier.sections.include_relations(:tables, :section_areas, :section_elements, product_categories: {products: :product_variants})
      #render json: JSONAPI::Serializer.serialize(current_supplier, serializer: Suppliers::SupplierSerializer, include: %w[
        #sections
        #sections.tables
        #sections.section_areas
        #sections.section_elements
        #product_categories
        #product_categories.products
        #product_categories.products.product_variants
      #]) #.new(current_supplier).as_json
      render json: current_supplier, include: %w[
        sections
        sections.tables
        sections.section_areas
        sections.section_elements
        product_categories
        product_categories.products
        product_categories.products.product_variants
      ]
    end

    def update
      @supplier = current_supplier
      current_supplier.update_attributes(supplier_params)
      render json: current_supplier
    end

    def switch_to
      @switch_supplier = Supplier.find(params[:id])
      session[:supplier_id] = params[:id] if @switch_supplier.employee_ids.include? current_employee.id
      redirect_to supplier_root_path(anchor: '/my-account')
    end

    def mark_as_open
      current_supplier.mark_as_open!
      head :ok
    end

    def mark_as_closed
      current_supplier.mark_as_closed!
      head :ok
    end

    private

    def supplier_params
      params.require(:supplier).permit(
        :name,
        :email,
        :open,
        :time_zone,
        :night_offset,
        :location,
        :lat,
        :lng,
        :offer_wifi,
        :wifi_ssid,
        :wifi_type,
        :wifi_password,
        :iens_profile,
        :address,
        :house_number,
        :house_number_addition,
        :postal_code,
        :city,
        :country,
        :facebook_promotion_url,
        :user_message
      )
    end
  end
end
