# rubocop:disable Metrics/ClassLength
require "httparty"

module Support
  class SplitIOClient
    class << self
      def add_user_to_segment_and_split(user, split)
        body = {
          "keys" => [
            user
          ]
        }
        set_environments unless @environment_id
        response = HTTParty.put(
          "#{ENV['SPLITIO_URL']}/segments/#{@environment_id}/cucumber_test_segment/uploadKeys",
          headers: @split_headers,
          body: body.to_json
        )
        puts JSON.parse(response.body)
        add_segment_to_split(split)
        @feature_name[user].push(split)
      end

      # rubocop:disable Metrics/MethodLength
      def remove_rule_from_split(user)
        body = [
          {
            "op" => "remove",
            "path" => "/rules/0"
          }
        ]
        @feature_name[user].each do |split|
          response = HTTParty.patch(
            "#{ENV['SPLITIO_URL']}/splits/ws/#{@workspace_id}/#{split}/environments/QA",
            headers: @split_headers,
            body: body.to_json
          )
          puts response.body
        end
        @feature_name.delete(user)
        puts @feature_name
      rescue NoMethodError
        puts "No feature flag recorded"
      end
      # rubocop:enable Metrics/MethodLength

      # rubocop:disable Metrics/MethodLength
      def add_user_to_split(user, split)
        body = [
          {
            "op" => "add",
            "path" => "/rules/0",
            "value" => {
              "buckets" => [
                {
                  "treatment" => "on",
                  "size" => 100
                }
              ],
              "condition" => {
                "matchers" => [
                  {
                    "type" => "IN_LIST_STRING",
                    "attribute" => "user_id",
                    "strings" => [
                      user.to_s
                    ]
                  }
                ]
              }
            }
          }
        ]
        set_environments unless @environment_id
        response = HTTParty.patch(
          "#{ENV['SPLITIO_URL']}/splits/ws/#{@workspace_id}/#{split}/environments/QA",
          headers: @split_headers,
          body: body.to_json
        )
        puts response.body
        puts "User #{user} added to #{split}"
        @feature_name[user].push(split)
        puts @feature_name
        sleep 9
      end
      # rubocop:enable Metrics/MethodLength

      def set_feature_hash
        @feature_name = Hash.new { |hsh, key| hsh[key] = [] }
      end

      def set_headers
        set_feature_hash
        @split_headers = {
          "Content-Type" => "application/json",
          "Authorization" => "Bearer " + ENV["SPLITIO_SDK_KEY"]
        }
      end

      def set_workspaces
        set_headers
        response = HTTParty.get(
          "#{ENV['SPLITIO_URL']}/workspaces", headers: @split_headers
        )
        @workspace_id = JSON.parse(response.body)["objects"][0]["id"]
        puts "Splitio workspace id: #{@workspace_id}"
      end

      def set_environments
        set_workspaces
        response = HTTParty.get(
          "#{ENV['SPLITIO_URL']}/environments/ws/#{@workspace_id}", headers: @split_headers
        )
        @environment_id = JSON.parse(response.body)[1]["id"]
        puts "Splitio environment id: #{@environment_id}"
      end

      # rubocop:disable Metrics/MethodLength
      def add_segment_to_split(split)
        body = [
          {
            "op" => "add",
            "path" => "/rules/0",
            "value" => {
              "buckets" => [
                {
                  "treatment" => "on",
                  "size" => 100
                }
              ],
              "condition" => {
                "matchers" => [
                  {
                    "type" => "IN_SEGMENT",
                    "string" => "cucumber_test_segment"
                  }
                ]
              }
            }
          }
        ]
        response = HTTParty.patch(
          "#{ENV['SPLITIO_URL']}/splits/ws/#{@workspace_id}/#{split}/environments/QA",
          headers: @split_headers,
          body: body.to_json
        )
        puts response.body
        puts "Segment 'cucumber_test_segment' added to split #{split} "
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end
# rubocop:enable Metrics/ClassLength
