module Support
  module Services
    class OwsUsers
      include HTTParty
      include URI

      base_uri DOMAINS[:ows_users]

      class << self
        def reset_auth0_user(user_id)
          auth0_user_id = get_auth0_user_id(user_id)
          path = URI.encode("/auth0/reset-users/" + auth0_user_id.to_s)
          headers = { "cache-control" => "no-cache" }

          response = delete(
            path,
            headers: headers
          )
          puts "User reset path: " + path.to_s
          puts "User id: " + user_id.to_s
          puts "User reset : " + response.to_s
          response.body
        end

        def reset_auth0_single_user(user_id)
          path = URI.encode("/auth0/reset-user/" + user_id)
          headers = { "cache-control" => "no-cache" }

          response = delete(
            path,
            headers: headers
          )
          puts "User reset path: " + path.to_s
          puts "User id: " + user_id.to_s
          puts "User reset : " + response.to_s
          response.body
        end

        def delete_auth0_user(user_id)
          auth0_user_id = get_auth0_user_id(user_id)
          path = URI.encode("/auth0/users/" + auth0_user_id.to_s)
          headers = { "cache-control" => "no-cache" }

          response = delete(
            path,
            headers: headers
          )
          puts "User delete path: " + path.to_s
          puts "User id: " + user_id.to_s
          puts "User deleted : " + response.code.to_s
          response.body
        end

        private

        def get_auth0_user_id(user_id)
          path = "/users/alw:" + user_id.to_s
          headers = { "accept" => "application/json" }

          puts "Auth0 user id path: " + path.to_s
          response = get(
            path,
            headers: headers
          )
          puts "User id: " + user_id.to_s
          puts "Auth0 User id: " + JSON.parse(response.body)["auth0_user_id"].to_s
          JSON.parse(response.body)["auth0_user_id"]
        end
      end
    end
  end
end
