#!/usr/bin/env bash
. "$(dirname "${BASH_SOURCE[0]}")/lib/harness.sh"

set -eo pipefail

ucode() {
  command ucode -L "$TACHYON_LIB" "$@"
}

PARSER="$ROOT_DIR/tachyon/files/usr/lib/subscription/parser.uc"
GENERATOR="$ROOT_DIR/tachyon/files/usr/lib/tachyon_core/generator.uc"

assert_contains() {
  local file="$1"
  local expected="$2"
  local label="$3"

  if ! grep -Fq "$expected" "$file"; then
    printf 'Output for %s:\n' "$label" >&2
    cat "$file" >&2
    fail "$label: expected to find $expected"
  fi
}

normalize_link() {
  local label="$1"
  local link="$2"
  local input="$WORK_DIR/$label.in"
  local output="$WORK_DIR/$label.json"

  printf '%s\n' "$link" >"$input"
  ucode "$PARSER" normalize-uri-list "$input" "$output"
  printf '%s\n' "$output"
}

allow_insecure_output="$(
  normalize_link \
    "hy2-allow-insecure" \
    "hysteria2://pa%3Ass@example.com:443?allowInsecure=1&sni=example.com&obfs=salamander&obfs-password=obf#hy2-allow"
)"
assert_contains "$allow_insecure_output" '"type": "hysteria2"' "hy2 allowInsecure type"
assert_contains "$allow_insecure_output" '"password": "pa:ss"' "hy2 allowInsecure password"
assert_contains "$allow_insecure_output" '"server_name": "example.com"' "hy2 allowInsecure SNI"
assert_contains "$allow_insecure_output" '"insecure": true' "hy2 allowInsecure TLS"
assert_contains "$allow_insecure_output" '"obfs": { "type": "salamander", "password": "obf" }' "hy2 allowInsecure obfs"

insecure_output="$(
  normalize_link \
    "hy2-insecure" \
    "hy2://pw@example.com:443?insecure=1#hy2-insecure"
)"
assert_contains "$insecure_output" '"insecure": true' "hy2 insecure TLS"

port_hopping_output="$(
  normalize_link \
    "hy2-port-hopping" \
    "hysteria2://pw@example.com:20000-40000?sni=example.com#hy2-hopping"
)"
assert_contains "$port_hopping_output" '"server_port": 20000' "hy2 port hopping initial server_port"
assert_contains "$port_hopping_output" '"server_ports": [' "hy2 port hopping server_ports"

mport_output="$(
  normalize_link \
    "hy2-mport" \
    "hysteria2://pw@example.com:443?mport=20000-40000&sni=example.com#hy2-mport"
)"
assert_contains "$mport_output" '"server_port": 443' "hy2 mport explicit server_port"
assert_contains "$mport_output" '"server_ports": [' "hy2 mport server_ports"

core_input="$WORK_DIR/core-hy2.json"
core_output="$WORK_DIR/core-hy2-normalized.json"
cat >"$core_input" <<'JSON'
{
  "outbounds": [
    {
      "type": "hysteria2",
      "tag": "core-hy2",
      "server": "example.com",
      "server_port": 443,
      "password": "pw",
      "tls": {
        "enabled": true,
        "server_name": "example.com",
        "alpn": [ "h3" ],
        "utls": {
          "enabled": true,
          "fingerprint": "chrome"
        }
      }
    }
  ]
}
JSON
ucode "$PARSER" normalize-content "$core_input" "$core_output"
assert_contains "$core_output" '"type": "hysteria2"' "tachyon-core HY2 type"
assert_contains "$core_output" '"alpn": [ "h3" ]' "tachyon-core HY2 ALPN"
if grep -Fq '"utls"' "$core_output"; then
  cat "$core_output" >&2
  fail "tachyon-core Hysteria2 normalization must drop TLS uTLS"
fi

mkdir -p "$WORK_DIR/subscriptions"
cp "$core_output" "$WORK_DIR/subscriptions/proxy-subscription-1.json"
printf '%s' 'https://example.com/sub.json' >"$WORK_DIR/subscriptions/proxy-subscription-1.url"
printf '%s' 'Happ' >"$WORK_DIR/subscriptions/proxy-subscription-1.user_agent"

generator_fixture="$WORK_DIR/generator-fixture.json"
generator_output="$WORK_DIR/generator-config.json"
cat >"$generator_fixture" <<'JSON'
{
  "settings": {
    ".name": "settings",
    ".type": "settings",
    "config_path": "/tmp/tachyon-core/config.json",
    "dns_server": "1.1.1.1",
    "service_listen_address": "127.0.0.1"
  },
  "section": [
    {
      ".name": "proxy",
      ".type": "section",
      "enabled": "1",
      "action": "proxy",
      "subscription_urls": [ "https://example.com/sub.json" ],
      "domain_suffix": [ "example.org" ]
    }
  ]
}
JSON
TMP_SUBSCRIPTION_FOLDER="$WORK_DIR/subscriptions" \
  ucode -L "$TACHYON_LIB" "$GENERATOR" generate-config-fixture \
    "$generator_fixture" "$generator_output" "127.0.0.1" "0"
assert_contains "$generator_output" '"type": "hysteria2"' "generated stale HY2 type"
if grep -Fq '"utls"' "$generator_output"; then
  cat "$generator_output" >&2
  fail "tachyon-core generator must drop stale Hysteria2 TLS uTLS from subscription cache"
fi

printf 'subscription Hysteria2 checks passed\n'
