#!/usr/bin/env bash
domain="com.mise.defaults-current-host-test.$$"
cat >"$MISE_CONFIG_DIR/config.toml" <<EOF_CONFIG
[[bootstrap.macos.defaults_entries]]
domain = "$domain"
key = "ScopeValue"
host = "current"
value = 1
[[bootstrap.macos.defaults_entries]]
domain = "$domain"
key = "GlobalOnly"
host = "current"
value = true
EOF_CONFIG
cat >mise.toml <<EOF_CONFIG
[bootstrap.macos.defaults."$domain"]
ScopeValue = false
[[bootstrap.macos.defaults_entries]]
domain = "$domain"
key = "ScopeValue"
host = "current"
value = 2
EOF_CONFIG
assert_contains 'mise bootstrap macos defaults status' '(current host)'
if [[ $(uname) != Darwin ]]; then
  require_cmd jq
  assert 'mise bootstrap macos defaults status --json | jq ".macos_defaults.entries | length"' 3
  assert 'mise bootstrap macos defaults status --json | jq -r ".macos_defaults.entries[0].host"' current
  mise bootstrap status --json | jq -c .macos_defaults >aggregate.json
  assert 'mise bootstrap macos defaults status --json | jq -c .macos_defaults' "$(cat aggregate.json)"
  assert_contains 'mise bootstrap macos defaults status' skipped
  assert_succeed 'mise bootstrap macos defaults apply --yes'
else
  require_cmd jq
  # Core Foundation writes outside the harness HOME, to the user's preference store.
  # Remove only this test's preference domain.
  trap 'defaults delete "$domain" >/dev/null 2>&1 || true; defaults -currentHost delete "$domain" >/dev/null 2>&1 || true' EXIT
  assert_contains 'mise bootstrap macos defaults apply --dry-run' "defaults -currentHost write $domain ScopeValue -int 2"
  assert 'mise bootstrap macos defaults status --json | jq ".macos_defaults.entries | length"' 3
  assert_succeed 'mise bootstrap macos defaults apply --yes'
  assert "defaults read '$domain' ScopeValue" 0
  assert "defaults -currentHost read '$domain' ScopeValue" 2
  assert "defaults -currentHost read '$domain' GlobalOnly" 1
  assert_succeed 'mise bootstrap macos defaults status --missing'
  assert 'mise bootstrap macos defaults status --json | jq "[.macos_defaults.entries[] | select(.host == \"current\")] | length"' 2
  assert_contains 'mise bootstrap macos defaults apply --yes 2>&1' '3 value(s) already set'
fi

# Explicit any-host entries override shorthand without wrapping dictionary values.
cat >mise.toml <<EOF_CONFIG
[bootstrap.macos.defaults."$domain"]
Dictionary = { host = "current", value = 1 }
ScopeValue = false
[[bootstrap.macos.defaults_entries]]
domain = "$domain"
key = "ScopeValue"
value = 7
EOF_CONFIG
assert_contains 'mise bootstrap macos defaults status' 'ScopeValue'
if [[ $(uname) == Darwin ]]; then
  assert_contains 'mise bootstrap macos defaults apply --dry-run' "defaults write $domain ScopeValue -int 7"
  assert 'mise bootstrap macos defaults status --json | jq -r ".macos_defaults.entries[] | select(.key == \"Dictionary\") | .value.host"' current
fi

cat >mise.toml <<EOF_CONFIG
[[bootstrap.macos.defaults_entries]]
domain = "$domain"
key = "ScopeValue"
host = "other"
value = 1
EOF_CONFIG
assert_fail 'mise bootstrap macos defaults status' 'unknown variant'

# Global-domain aliases merge across shorthand and explicit entries in both directions.
for alias in -g -globalDomain; do
  cat >"$MISE_CONFIG_DIR/config.toml" <<EOF_CONFIG
[bootstrap.macos.defaults."$alias"]
"_mise_host_alias_test_$$" = 1
EOF_CONFIG
  cat >mise.toml <<EOF_CONFIG
[[bootstrap.macos.defaults_entries]]
domain = "NSGlobalDomain"
key = "_mise_host_alias_test_$$"
value = 2
EOF_CONFIG
  if [[ $(uname) == Darwin ]]; then
    assert 'mise bootstrap macos defaults status --json | jq -c "[.macos_defaults.entries[].value]"' '[2]'
  fi
  cat >"$MISE_CONFIG_DIR/config.toml" <<EOF_CONFIG
[[bootstrap.macos.defaults_entries]]
domain = "NSGlobalDomain"
key = "_mise_host_alias_test_$$"
value = 1
EOF_CONFIG
  cat >mise.toml <<EOF_CONFIG
[bootstrap.macos.defaults."$alias"]
"_mise_host_alias_test_$$" = 2
EOF_CONFIG
  if [[ $(uname) == Darwin ]]; then
    assert 'mise bootstrap macos defaults status --json | jq -c "[.macos_defaults.entries[].value]"' '[2]'
  fi
done
