class Api::V1::ProvisioningApsController < Api::V1::ApiController
	
	def create
		user = User.where(access_token: params[:access_token]).first
		if params[:mac_id].present? && user.present?
			already_exists = []
			router, status = RouterInventory.router_activation([params[:mac_id],params[:network_name]],user,false)
			already_exists << router unless status
			unless already_exists.present?
        flash[:success] = "Successfully created new Device"
        render json: { msg: "Successfully created new Devices", status: 200 }
      else
        render json: { msg: "Following mac_ids <b> #{already_exists.join(",")}</b> of devices were not been added", status: 401 }
      end
		else
			render json: { msg: "Invalid user access token or mac id is not present", status: 401 }
		end
	end

	def create_master_inventory
		#user = User.where(access_token: params[:access_token]).first
		#if params[:mac_id].present? && user.present?
		if params[:mac_id].present?
      macids = params[:mac_id].split(",")
      part_number = params[:part_number]
      hardware_part = HardwarePart.where(part_number: part_number).last
      if hardware_part.present?
	      macids.each do |mac|
	       	master_router = MasterRouterInventory.where(mac_id: mac.upcase).last
	       	unless master_router.present?
	          MasterRouterInventory.create(mac_id: mac.upcase, hardware_part_id: hardware_part.id)
	      	else
	      		master_router.hardware_part_id = hardware_part.id
	      		master_router.save
	      	end
	     	end
     	else
      	errormsg = "Hardware part number is Invalid"
     	end
			unless errormsg.present?
        render json: { msg: "Successfully created new Devices", status: 200 }
      else
        render json: { msg: errormsg, status: 422 }
      end
    else
      # render json: { msg: "Invalid user access token or mac id is not present", status: 401 }
      render json: { msg: "Please provide atleast one MAC address", status: 422 }
    end
  end

  def enable_telemetry
    if params[:mac_ids].present?
      mac_ids = params[:mac_ids].split(',')
      ris = RouterInventory.where(mac_id: mac_ids, vendor_type: "4")

      ris.each do |ri|
        ri.enable_telemetry
      end

      render json: { msg: "Successfully enabled telemetry for Devices", status: 200 }
    else
      render json: { msg: "Please provide atleast one MAC address", status: 422 }
    end
  end
end