#!/usr/bin/env bash
# Section group (action 'group'): one flat URLTest over the union of member
# leaves, member route rules repointed at that URLTest, members validated.
. "$(dirname "${BASH_SOURCE[0]}")/lib/harness.sh"
set -eo pipefail

VALIDATOR="$TACHYON_LIB/config/validator.uc"
GENERATOR_UC="$TACHYON_LIB/tachyon_core/generator.uc"

# validator warnings go through logger(1); stub it so accepted fixtures do not
# depend on a host logger
mkdir -p "$WORK_DIR/bin"
cat >"$WORK_DIR/bin/logger" <<'BASH'
#!/usr/bin/env bash
exit 0
BASH
chmod +x "$WORK_DIR/bin/logger"

# ─── validator ────────────────────────────────────────────────────────────────

fixture() {
  local out="$1" sections="$2"
  cat >"$out" <<JSON
{
  "settings": {
    ".name": "settings",
    ".type": "settings",
    "list_update_enabled": "1",
    "update_interval": "1d",
    "latency_test_url": "https://latency.example/generate_204",
    "dns_type": "doh",
    "dns_server": [ "dns.google/dns-query" ],
    "bootstrap_dns_server": [ "1.1.1.1" ]
  },
  "section": $sections
}
JSON
}

validate_fixture() {
  local source="$1"
  TACHYON_LIB="$TACHYON_LIB" PATH="$WORK_DIR/bin:$PATH" \
    ucode -L "$TACHYON_LIB" "$VALIDATOR" validate-runtime-fixture "$source" "$WORK_DIR/context.json"
}
printf '{}\n' >"$WORK_DIR/context.json"

assert_accepts() {
  local label="$1" fixture="$2" output
  if ! output="$(validate_fixture "$fixture" 2>&1)"; then
    fail "$label should be accepted, got '$output'"
  fi
}

assert_rejects() {
  local label="$1" fixture="$2" expected="$3" output
  if output="$(validate_fixture "$fixture" 2>&1)"; then
    fail "$label should be rejected"
  fi
  printf '%s\n' "$output" | grep -Fq "$expected" ||
    fail "$label: expected message containing '$expected', got '$output'"
}

LINK_A1='vless://00000000-0000-4000-8000-000000000001@alpha.example:443?encryption=none&security=tls&sni=alpha.example#Alpha%20One'
LINK_A2='vless://00000000-0000-4000-8000-000000000002@alpha2.example:443?encryption=none&security=tls&sni=alpha2.example#Alpha%20Two'
LINK_B1='vless://00000000-0000-4000-8000-000000000003@beta.example:443?encryption=none&security=tls&sni=beta.example#Beta%20One'

SECTIONS_OK='[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "domain_suffix": [ "alpha.example" ],
    "selector_proxy_links": [ "'"$LINK_A1"'", "'"$LINK_A2"'" ] },
  { ".name": "beta", ".type": "section", "enabled": "1", "action": "connection",
    "domain_suffix": [ "beta.example" ],
    "selector_proxy_links": [ "'"$LINK_B1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "alpha", "beta" ] }
]'

fixture "$WORK_DIR/group-ok.json" "$SECTIONS_OK"
# stash direction: without the action whitelist this fixture is rejected with
# "unsupported action 'group'", so the acceptance below fails on old code
assert_accepts "a group over two connection sections" "$WORK_DIR/group-ok.json"

fixture "$WORK_DIR/group-missing.json" '[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "alpha", "nope" ] }
]'
assert_rejects "group with missing member" "$WORK_DIR/group-missing.json" \
  "references missing rule 'nope'"

fixture "$WORK_DIR/group-disabled.json" '[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "beta", ".type": "section", "enabled": "0", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_B1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "beta" ] }
]'
assert_rejects "group over a disabled member" "$WORK_DIR/group-disabled.json" \
  "references disabled rule 'beta'"

fixture "$WORK_DIR/group-partial-disabled.json" '[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "beta", ".type": "section", "enabled": "0", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_B1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "alpha", "beta" ] }
]'
assert_accepts "group with one enabled and one disabled member" "$WORK_DIR/group-partial-disabled.json"

fixture "$WORK_DIR/group-blank-enabled.json" '[
  { ".name": "amnezia", ".type": "section", "enabled": "", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "amnezia" ] }
]'
assert_accepts "group with member having empty enabled string" "$WORK_DIR/group-blank-enabled.json"

fixture "$WORK_DIR/group-self.json" '[
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "grp" ] }
]'
assert_rejects "group including itself" "$WORK_DIR/group-self.json" \
  "cannot include itself"

fixture "$WORK_DIR/group-empty.json" '[
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [] }
]'
assert_rejects "group without members" "$WORK_DIR/group-empty.json" \
  "has no members"

fixture "$WORK_DIR/group-duplicate.json" '[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "alpha", "alpha" ] }
]'
assert_rejects "group with a duplicate member" "$WORK_DIR/group-duplicate.json" \
  "lists rule 'alpha' twice"

fixture "$WORK_DIR/group-route-only.json" '[
  { ".name": "local", ".type": "section", "enabled": "1", "action": "hosts" },
  { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "local" ] }
]'
assert_rejects "group over a route-only member" "$WORK_DIR/group-route-only.json" \
  "cannot include rule 'local' with action 'hosts'"

fixture "$WORK_DIR/group-nested.json" '[
  { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
    "selector_proxy_links": [ "'"$LINK_A1"'" ] },
  { ".name": "inner", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "alpha" ] },
  { ".name": "outer", ".type": "section", "enabled": "1", "action": "group",
    "group_members": [ "inner" ] }
]'
assert_rejects "nested groups" "$WORK_DIR/group-nested.json" \
  "with action 'group'"

# ─── generator: flat urltest + repointed member rules ─────────────────────────

cat >"$WORK_DIR/group-fixture.json" <<JSON
{
  "settings": { ".name": "settings", ".type": "settings", "log_level": "warn" },
  "section": $SECTIONS_OK
}
JSON

group_config="$WORK_DIR/group-config.json"
mkdir -p "${group_config}.section-cache"
ucode -L "$TACHYON_LIB" "$GENERATOR_UC" generate-config-fixture \
  "$WORK_DIR/group-fixture.json" "$group_config" "127.0.0.1"

ucode -e '
let fs = require("fs");
function outbound_by_tag(config, want) {
    for (let outbound in config.outbounds || [])
        if (outbound && outbound.tag == want)
            return outbound;
    return null;
}
function contains(values, needle) {
    for (let value in values || [])
        if (value == needle)
            return true;
    return false;
}
let config = json(fs.readfile(ARGV[0]));

let group = outbound_by_tag(config, "grp-urltest-out");
if (!group || group.type != "urltest")
    die("the group section must produce a grp-urltest-out outbound\n");
for (let want in [ "alpha-1-out", "alpha-2-out", "beta-1-out" ])
    if (!contains(group.outbounds || [], want))
        die("flat group urltest is missing member leaf " + want + "\n");
if (length(group.outbounds || []) != 3)
    die("flat group urltest must be exactly the union of member leaves, got " +
        length(group.outbounds || []) + "\n");

let group_sel = outbound_by_tag(config, "grp-out");
if (!group_sel || group_sel.type != "selector" || group_sel.default != "grp-urltest-out")
    die("the group section must produce a grp-out selector defaulting to grp-urltest-out\n");
for (let want in [ "alpha-1-out", "alpha-2-out", "beta-1-out", "grp-urltest-out" ])
    if (!contains(group_sel.outbounds || [], want))
        die("grp-out selector is missing " + want + "\n");

// merge semantics keep member selectors — they stay usable as detour targets
if (!outbound_by_tag(config, "alpha-out") || !outbound_by_tag(config, "beta-out"))
    die("member section selectors must survive the merge\n");

let alpha_rule = null;
let beta_rule = null;
let group_rule_count = 0;
for (let rule in config.route.rules || []) {
    if (rule.outbound == "grp-out")
        group_rule_count++;
    if (contains(rule.domain_suffix || [], "alpha.example"))
        alpha_rule = rule;
    if (contains(rule.domain_suffix || [], "beta.example"))
        beta_rule = rule;
}
if (!alpha_rule || !beta_rule)
    die("member routing rules were not generated\n");
if (alpha_rule.outbound != "grp-out" || beta_rule.outbound != "grp-out")
    die("member rules must route through the group selector grp-out, got alpha=" +
        (alpha_rule.outbound || "null") + " beta=" + (beta_rule.outbound || "null") + "\n");
if (group_rule_count != 2)
    die("the group itself must not add route rules when it has no conditions; expected 2, got " + group_rule_count + "\n");

let grp_cache = json(fs.readfile(ARGV[0] + ".section-cache/grp.json"));
if (!grp_cache || type(grp_cache.outboundMetadata) != "object" || type(grp_cache.outboundMetadata.names) != "object")
    die("group section cache grp.json must be written with outboundMetadata.names\n");
for (let want in [ "alpha-1-out", "alpha-2-out", "beta-1-out" ])
    if (!grp_cache.outboundMetadata.names[want])
        die("group section cache grp.json is missing metadata for " + want + "\n");
' "$group_config" || fail "generated section group behavior"

# ─── generator: routing conditions on group section + mixed proxy + filter ────

cat >"$WORK_DIR/group-direct-rules-fixture.json" <<JSON
{
  "settings": { ".name": "settings", ".type": "settings", "log_level": "warn" },
  "section": [
    { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
      "selector_proxy_links": [ "$LINK_A1", "$LINK_A2" ],
      "dashboard_filter_mode": "exclude",
      "dashboard_exclude_outbounds": [ "Alpha Two" ] },
    { ".name": "beta", ".type": "section", "enabled": "1", "action": "connection",
      "selector_proxy_links": [ "$LINK_B1" ] },
    { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
      "group_members": [ "alpha", "beta" ],
      "domain_suffix": [ "group-direct.example" ],
      "mixed_proxy_enabled": "1",
      "mixed_proxy_port": "4599" }
  ]
}
JSON

group_direct_config="$WORK_DIR/group-direct-config.json"
mkdir -p "${group_direct_config}.section-cache"
ucode -L "$TACHYON_LIB" "$GENERATOR_UC" generate-config-fixture \
  "$WORK_DIR/group-direct-rules-fixture.json" "$group_direct_config" "127.0.0.1"

ucode -e '
let fs = require("fs");
function outbound_by_tag(config, want) {
    for (let outbound in config.outbounds || [])
        if (outbound && outbound.tag == want)
            return outbound;
    return null;
}
function contains(values, needle) {
    for (let value in values || [])
        if (value == needle)
            return true;
    return false;
}
let config = json(fs.readfile(ARGV[0]));
let group = outbound_by_tag(config, "grp-urltest-out");
if (!group || group.type != "urltest")
    die("grp-urltest-out missing\n");
if (contains(group.outbounds || [], "alpha-2-out"))
    die("filtered-out member leaf alpha-2-out must not be in grp-urltest-out\n");
for (let want in [ "alpha-1-out", "beta-1-out" ])
    if (!contains(group.outbounds || [], want))
        die("expected leaf " + want + " in grp-urltest-out\n");

let direct_rule = null;
let mixed_rule = null;
for (let rule in config.route.rules || []) {
    if (contains(rule.domain_suffix || [], "group-direct.example"))
        direct_rule = rule;
    if ((rule.inbound == "grp-mixed-in" || contains(rule.inbound || [], "grp-mixed-in")) && !rule.ip_is_private)
        mixed_rule = rule;
}
if (!direct_rule || direct_rule.outbound != "grp-out")
    die("routing conditions on the group section itself must route to grp-out\n");
if (!mixed_rule || mixed_rule.outbound != "grp-out")
    die("mixed proxy on the group section must route to grp-out\n");

let grp_direct_cache = json(fs.readfile(ARGV[0] + ".section-cache/grp.json"));
if (!grp_direct_cache || type(grp_direct_cache.links) != "object")
    die("grp.json must exist for group-direct-config\n");
if (grp_direct_cache.links["alpha-2-out"] || (grp_direct_cache.servers && grp_direct_cache.servers["alpha-2-out"]) ||
    (grp_direct_cache.outboundMetadata && grp_direct_cache.outboundMetadata.names && grp_direct_cache.outboundMetadata.names["alpha-2-out"]))
    die("excluded member leaf alpha-2-out must not leak into grp.json links/servers/outboundMetadata\n");
if (!grp_direct_cache.links["alpha-1-out"] || !grp_direct_cache.links["beta-1-out"])
    die("included member leaves alpha-1-out and beta-1-out must be present in grp.json links\n");
' "$group_direct_config" || fail "generated section group direct rules, filter, and mixed proxy"

# ─── generator: manual action=awg endpoint included in group urltest ──────────

cat >"$WORK_DIR/group-awg-fixture.json" <<JSON
{
  "settings": { ".name": "settings", ".type": "settings", "log_level": "warn" },
  "section": [
    { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
      "domain_suffix": [ "alpha.example" ],
      "selector_proxy_links": [ "$LINK_A1" ] },
    { ".name": "amnezia", ".type": "section", "enabled": "1", "action": "awg",
      "domain_suffix": [ "awg.example" ],
      "awg_private_key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
      "awg_peer_public_key": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=",
      "awg_server_address": "198.51.100.10",
      "awg_server_port": "51820",
      "awg_local_address": [ "10.8.1.2/32" ] },
    { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
      "group_members": [ "alpha", "amnezia" ] }
  ]
}
JSON

group_awg_config="$WORK_DIR/group-awg-config.json"
mkdir -p "${group_awg_config}.section-cache"
ucode -L "$TACHYON_LIB" "$GENERATOR_UC" generate-config-fixture \
  "$WORK_DIR/group-awg-fixture.json" "$group_awg_config" "127.0.0.1"

ucode -e '
let fs = require("fs");
function outbound_by_tag(config, want) {
    for (let outbound in config.outbounds || [])
        if (outbound && outbound.tag == want)
            return outbound;
    return null;
}
function endpoint_by_tag(config, want) {
    for (let ep in config.endpoints || [])
        if (ep && ep.tag == want)
            return ep;
    return null;
}
function contains(values, needle) {
    for (let value in values || [])
        if (value == needle)
            return true;
    return false;
}
let config = json(fs.readfile(ARGV[0]));
let ep = endpoint_by_tag(config, "amnezia-out");
if (!ep || ep.type != "wireguard")
    die("manual action=awg section must produce a wireguard endpoint amnezia-out\n");
let group = outbound_by_tag(config, "grp-urltest-out");
if (!group || group.type != "urltest")
    die("group over connection + manual awg must produce grp-urltest-out\n");
for (let want in [ "alpha-1-out", "amnezia-out" ])
    if (!contains(group.outbounds || [], want))
        die("group urltest is missing member leaf " + want + "\n");
let awg_rule = null;
for (let rule in config.route.rules || [])
    if (contains(rule.domain_suffix || [], "awg.example"))
        awg_rule = rule;
if (!awg_rule || awg_rule.outbound != "grp-out")
    die("manual awg member route rule must be repointed to grp-out, got " +
        (awg_rule ? awg_rule.outbound : "null") + "\n");
' "$group_awg_config" || fail "generated section group with manual awg member"

# ─── generator: section group with outbound_group_refs narrows member pool (#395) ──

cat >"$WORK_DIR/group-pool-refs-fixture.json" <<JSON
{
  "settings": { ".name": "settings", ".type": "settings", "log_level": "warn" },
  "outbound_group": [
    { ".name": "grp_alpha_only", ".type": "outbound_group", "enabled": "1",
      "name": "Alpha Only", "filter_mode": "include",
      "include_regex": [ "^Alpha" ] }
  ],
  "section": [
    { ".name": "alpha", ".type": "section", "enabled": "1", "action": "connection",
      "domain_suffix": [ "alpha.example" ],
      "selector_proxy_links": [ "$LINK_A1", "$LINK_A2" ] },
    { ".name": "beta", ".type": "section", "enabled": "1", "action": "connection",
      "domain_suffix": [ "beta.example" ],
      "selector_proxy_links": [ "$LINK_B1" ] },
    { ".name": "grp", ".type": "section", "enabled": "1", "action": "group",
      "group_members": [ "alpha", "beta" ],
      "outbound_group_refs": [ "grp_alpha_only" ] }
  ]
}
JSON

group_pool_refs_config="$WORK_DIR/group-pool-refs-config.json"
mkdir -p "${group_pool_refs_config}.section-cache"
ucode -L "$TACHYON_LIB" "$GENERATOR_UC" generate-config-fixture \
  "$WORK_DIR/group-pool-refs-fixture.json" "$group_pool_refs_config" "127.0.0.1"

ucode -e '
let fs = require("fs");
function outbound_by_tag(config, want) {
    for (let outbound in config.outbounds || [])
        if (outbound && outbound.tag == want)
            return outbound;
    return null;
}
function contains(values, needle) {
    for (let value in values || [])
        if (value == needle)
            return true;
    return false;
}
let config = json(fs.readfile(ARGV[0]));
let group = outbound_by_tag(config, "grp-urltest-out");
if (!group || group.type != "urltest")
    die("group with outbound_group_refs must produce grp-urltest-out\n");
if (!contains(group.outbounds || [], "alpha-1-out") || !contains(group.outbounds || [], "alpha-2-out"))
    die("group urltest must retain Alpha servers matched by outbound_group_refs\n");
if (contains(group.outbounds || [], "beta-1-out"))
    die("group urltest must exclude beta-1-out when outbound_group_refs restricts to Alpha (#395)\n");
' "$group_pool_refs_config" || fail "generated section group with outbound_group_refs (#395)"

# ─── capability: tachyon-core supports sections.group ─────────────────────────

parking="$(ucode -L "$TACHYON_LIB" -e '
let st = require("components.engine_state");
let e = require("core.engine");
function has(values, needle) {
    for (let value in values || [])
        if (value == needle)
            return true;
    return false;
}
printf("feat=%s\n", st.section_feature({ ".type": "section", "action": "group" }));
printf("core=%d\n", has(e.capabilities("tachyon-core"), "sections.group"));
' 2>&1)" || fail "could not read group parking metadata: $parking"

grep -q '^feat=sections.group$' <<<"$parking" ||
  fail "a group section must map to the sections.group feature: $parking"
grep -q '^core=1$' <<<"$parking" ||
  fail "tachyon-core capabilities must include sections.group: $parking"

# ─── UI: action is choosable and members are pickable ─────────────────────────

SECTION_JS="$ROOT_DIR/luci-app-tachyon/htdocs/luci-static/resources/view/tachyon/section.js"
[ -f "$SECTION_JS" ] || fail "section.js not found at $SECTION_JS"
grep -Fq 'option.value("group", getActionOptionLabel("group"))' "$SECTION_JS" ||
  fail "the action chooser must offer the group action"
grep -Fq '"group_members"' "$SECTION_JS" ||
  fail "the section modal must declare the group_members picker"
grep -Fq 'getOutboundDetourTargetSections(section_id).map' "$SECTION_JS" ||
  fail "the group members picker must reuse the eligible-section choices"

printf 'Section group checks passed\n'
