# frozen_string_literal: true

require_relative "../base"
require "byebug"
module Neetob
  class CLI
    module Cloudflare
      class Base < CLI::Base
        NEETO_DEPLOY_DOCS = "https://github.com/bigbinary/neetob/#working-with-neetodeploy"

        ZONE_IDS = {
          "neetoauth.com": "81ade5aa3e075489533f903dc03b1e88",
          "neetoauth.net": "4260febb059f04068fa0105765d9ef39",
          "neetocal.com": "40a8ce54e49b2abf6c113b06efc33caf",
          "neetocal.net": "899c96f5c95884c96c8f40574749e6b0",
          "neetocentral.com": "94d1a30430c247be253dd131bbf75994",
          "neetocentral.net": "fce73845e62b663124cf51190bdfc2b8",
          "neetoengage.com": "deeccc1424c6732db8d372522a7254ea",
          "neetoengage.net": "c0330ba264a485e76e9b88bde90f7583",
          "neetochat.com": "17295de45a5455d02725f337e60e5664",
          "neetochat.net": "1bcdc8e62717e703cf2f6954e6525e20",
          "neetochatify.com": "021482cc245d96cd8806ef19c20d6b10",
          "neetochatify.net": "31e3445557656d5a6f732bbb7de9bb5a",
          "neetoci.com": "3faaaced0e91e08eb2d613fbffdad490",
          "neetoci.net": "dd205596095ed10d7776c3a057a53f46",
          "neetocode.com": "0359dcaec1119b18dc33a217123dbdb1",
          "neetocode.net": "9180dab7f7071c9f845a33e19121c3a9",
          "neetocourse.com": "f5e2995e841578a263a1a2e378e60d07",
          "neetocourse.net": "471a8f99942950ad2eea8f2cbbae38c0",
          "neetocoursesite.com": "b131662b2c97cf1982f32bac89f005cf",
          "neetocoursesite.net": "036a7fb612edc71b10363c331f7f9f73",
          "neetocrm.com": "76f3aae935e58b1d9277f05299950c3d",
          "neetocrm.net": "aa5fca152b1f925119d5819e4c58340e",
          "neetodeploy.com": "cc43d7c29dceec872cd2a51a5389bc32",
          "neetodeploy.net": "01329b72a46fb30e6f12981c398c4728",
          "neetodesk.com": "eabcea3740bb428ce0161e3dccad5a33",
          "neetodesk.net": "af812f4ea35348eaece3b499988d1cd3",
          "neetoform.com": "71ff2cbde5d472a9d987298cc2af50f6",
          "neetoform.net": "6af6e63021fba23eed48100ad440479a",
          "neetogit.com": "4925445a663a4de68eaf6e959243aaad",
          "neetogit.net": "a4a850fb571785305ca517949a4efdb9",
          "neetogrow.com": "784cda433030c1475b7a32295e98feb7",
          "neetogrow.net": "67a171f16ae95e6435b1789f203a7853",
          "neetoinvoice.com": "0394b5ce1d094459ca700fa051f01d60",
          "neetoinvoice.net": "4c864591b2b986abcb6de133b6c3815d",
          "neetokb.com": "25804f331677e305adb550cba3a672dc",
          "neetokb.net": "c8aa648cf3bd886d77c4ffb2288fdb53",
          "neetoplanner.com": "680db9b8804ea976581109c800f83721",
          "neetoplanner.net": "ebe6db8f92b31e8bf086ae8e961955d1",
          "neetoplaydash.com": "12c88df4f0bfdb29a959f1840abea512",
          "neetoplaydash.net": "67e2a4c3d4a71f3ae83cfa35d9e18925",
          "neetopublish.com": "d66450d159922e7f704c2a9a46c6256c",
          "neetopublish.net": "af9c5ea6b0c293a9831ddbe1dec2082f",
          "neetoquiz.com": "864154a83f452dd62b1331c8d7d125e0",
          "neetoquiz.net": "608320f45d132f7f49fca17ba5a96bde",
          "neetorecord.com": "8b8173c36c932ce2e66d15101a16b227",
          "neetorecord.net": "7ec3958aab651f9a8c1a4422ca4521b8",
          "neetoreplay.com": "dcec9a107cc7b39a67690827d0712b88",
          "neetoreplay.net": "8eb99a1f88e28b07dc965aa807a1b69a",
          "neetorunner.com": "9727602fd57ab5c63645df341a0c79c1",
          "neetorunner.net": "8b5b81e94e1238cb7d8bd4062c788233",
          "neetosite.com": "10a07b451a511a57513a74d40ae3680d",
          "neetosite.net": "23064d6c435af82b92a102b465d5faf6",
          "neetosurvey.com": "a0dfcaec5d88a2e99500bdc4957eba27",
          "neetosurvey.net": "96edf6d04a4666b6b1bef507a17c958b",
          "neetotestify.com": "b2d592bc096b4c1b80c6941bd1a8597c",
          "neetotestify.net": "1bbb5bf2ea04121c57a3690d2f40c5dc",
          "neetotower.com": "9175d2e1bb19735b9b22c101ac162d31",
          "neetotower.net": "10f9eba21876cd174952b830aba0ba71",
          "neetotribe.com": "45eb5b7d96820cf7c375dcdaf608d4c7",
          "neetotribe.net": "f49cb0229b1d67d228da7ab39a17dde3",
          "neetowireframe.com": "d6d17a3dab91dc0ca4b9f4f65b086ccc",
          "neeto.com": "83eeffbce76df68b60e50d1fa254b3fa",
          "bigbinary.com": "dc60ee7ecbff0ef5e58e1bb27ad40e89"
        }

        def initialize
          super()
        end

        def process(*args)
          raise(StandardError, "CLOUDFLARE_API_KEY is not given") if ENV["CLOUDFLARE_API_KEY"].nil?

          run(*args)
        end

        private

          def create_url(zone_id, endpoint)
            "https://api.cloudflare.com/client/v4/zones/#{zone_id}/#{endpoint}"
          end

          def parse_response(http_result)
            case http_result
            when Net::HTTPSuccess
              JSON.parse(http_result.body, symbolize_names: true)
            else
              error_message = JSON.parse(http_result.body)["errors"][0]["message"]
              raise(StandardError, "Request failed with status code #{http_result.code}: #{error_message}")
            end
          end

          def get(url, headers: { "Accept" => "application/json", "Authorization" => ENV["CLOUDFLARE_API_KEY"] })
            uri = URI(url)
            request = Net::HTTP::Get.new(uri)
            headers.each { |key, value| request[key] = value }

            response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
              http.request(request)
            end

            parse_response(response)
          end
      end
    end
  end
end