-- Rewrite this all to sh and include it in plsh (use psql -Atc if need some database queries)
#define mock__user_id 380055934
#define mock__password password
#define curl /run/current-system/sw/bin/curl
#define jq /run/current-system/sw/bin/jq
#define mock__api_key '89413671570107726acaf368efcbfb076f44f34d94651a2309e15bbb1f4adf0c'
#define mock__base_url 'vproxy.store/reseller-api'
#define wrap_curl_output  --write-out '\n%{http_code}' | { \
    IFS= read -r body; \
    IFS= read -r code; \
    if echo "$body" | jq empty >/dev/null 2>&1; then \
      jq -n --argjson body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; \
    else \
      jq -n --arg body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; \
    fi \
}

#define binsh /run/current-system/sw/bin/sh

BEGIN;
  CREATE TABLE pg_temp.state (
    key text,
    value text
  ) INHERITS (common.created_at);

  CREATE FUNCTION pg_temp."balance/get"() 
  RETURNS jsonb 
  AS $$#!binsh
  curl \
    --silent \
    --location 'https://'mock__base_url'/balance/get' \
    --header   'apikey: 'mock__api_key \
    wrap_curl_output
  $$ LANGUAGE plsh;

  CREATE FUNCTION pg_temp."balance/get_full"() 
  RETURNS jsonb 
  AS $$#!binsh
  curl \
    --silent \
    --location 'https://'mock__base_url'/balance/get_full' \
    --header   'apikey: 'mock__api_key \
    wrap_curl_output
  $$ LANGUAGE plsh;

  CREATE FUNCTION pg_temp.valicate_curl_output(result JSONB, expected_code integer)
  RETURNS void
  AS $$
  BEGIN
    IF (result->>'code')::integer != expected_code THEN
      RAISE EXCEPTION 'Expected code %, but got %', expected_code, result->>'code';
    END IF;
  END;
  $$ LANGUAGE plpgsql;

  CREATE FUNCTION pg_temp."subuser/create"(pool_type text, threads integer, sticky_range jsonb)
  RETURNS jsonb
  AS $$#!binsh
    pool_type="${1}"
    threads="${2}"
    sticky_range="${3}"
    if [ -z "${sticky_range}" ]; then
      sticky_range="null"
    fi

    curl \
      -X POST \
      --silent \
      --location 'https://'mock__base_url'/subuser/create' \
      --header 'apikey: 'mock__api_key \
      --header 'Content-Type: application/json' \
      --data '{
        "pool_type": "'"${pool_type}"'",
        "sticky_range": '"${sticky_range}"',
        "threads": '"${threads}"',
        "allowed_ips": [],
        "default_pool_parameters": {
          "rotation_interval": 120
        }
      }' \
      --write-out '\n%{http_code}' | { 
          IFS= read -r body; 
          IFS= read -r code; 
          if echo "$body" | jq empty >/dev/null 2>&1; then 
            jq -n --argjson body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; 
          else 
            jq -n --arg body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; 
          fi 
      }
  $$ LANGUAGE plsh;

  -- ' this is for fix treesiter :)

  CREATE FUNCTION pg_temp."subuser/get"(subuser_id text)
  RETURNS jsonb
  AS $$#!binsh
  subuser_id="${1}"
  curl \
    --silent \
    --location 'https://'mock__base_url"/subuser/get?subuser_id=$subuser_id" \
    --header 'apikey: 'mock__api_key \
    wrap_curl_output
  $$ LANGUAGE plsh;

  CREATE FUNCTION pg_temp."subuser/pool/parameters"(subuser_id text)
  RETURNS jsonb
  AS $$#!binsh
  subuser_id="${1}"
  curl \
    -X POST \
    --silent \
    --location 'https://'mock__base_url'/subuser/pool/parameters' \
    --header 'apikey: 'mock__api_key \
    --header 'Content-Type: application/json' \
    --data '{
      "subuser_id": "'"${subuser_id}"'",
      "parameters": {
        "rotation_interval": 80
      }
    }' \
    --write-out '\n%{http_code}' | { 
        IFS= read -r body; 
        IFS= read -r code; 
        if echo "$body" | jq empty >/dev/null 2>&1; then 
          jq -n --argjson body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; 
        else 
          jq -n --arg body "$body" --argjson code "$code" '{"body": $body, "code": $code}'; 
        fi 
    }
  $$ LANGUAGE plsh;

  -- '

  DO $$
    DECLARE
      current_result JSONB;
      balance_gb int;
      subuser_id text;
    BEGIN
      current_result := pg_temp."balance/get"();
      PERFORM pg_temp.valicate_curl_output(current_result, 200);

      current_result := pg_temp."balance/get_full"();
      PERFORM pg_temp.valicate_curl_output(current_result, 200);

      balance_gb := (current_result->'body'->>'gb')::int;

      IF balance_gb < 10 THEN
        RAISE WARNING 'Balance not enough for test';
        UPDATE reseller_inner.resellers SET traffic_amount = reseller_inner.bytes_to_gb(10) WHERE api_key = mock__api_key;
        RAISE NOTICE 'Updated balance to 10GB';
      END IF;

      RAISE NOTICE 'Creating subuser....';
      current_result := pg_temp."subuser/create"('residential', 1, null);
      current_result := pg_temp."subuser/create"('residential', 1000, '{"start": "10000", "end": "20000"}');

      subuser_id := (current_result->>'body')::int;

      RAISE NOTICE 'Subuser created: %', pg_temp."subuser/get"(subuser_id);

      current_result := pg_temp."subuser/pool/parameters"(subuser_id);

      RAISE NOTICE 'Subuser pool parameters updated: %', current_result;
    END;
  $$ LANGUAGE plpgsql;
END;