#!/usr/bin/env bash

export MISE_EXPERIMENTAL=1
export MISE_USE_VERSIONS_HOST=0

MISE_DEBUG=1 mise use swift@6
assert_matches "mise x -- swift --version" 'Swift version 6\.'

# TODO: validate these older SwiftFormat source releases with current Swift toolchains.
#assert "mise x spm:nicklockwood/SwiftFormat@0.53.10 -- swiftformat --version" "0.53.10"
#assert "mise x spm:https://github.com/nicklockwood/SwiftFormat.git@0.53.10 -- swiftformat --version" "0.53.10"

assert "mise x spm:nicklockwood/SwiftFormat@0.61.1 -- swiftformat --version" "0.61.1"

assert_matches "mise x -- swift package --version" 'Swift Package Manager - Swift 6\.'

# A commit selector must be checked out as a Git revision rather than being
# converted into a release tag named `rev:<sha>`.
mkdir -p spm-revision/Sources/revision-tool
cat >spm-revision/Package.swift <<'EOF'
// swift-tools-version: 5.9
import PackageDescription

let package = Package(
    name: "revision-tool",
    products: [.executable(name: "revision-tool", targets: ["revision-tool"])],
    targets: [.executableTarget(name: "revision-tool")]
)
EOF
echo 'print("first commit")' >spm-revision/Sources/revision-tool/main.swift
git -C spm-revision init -q
git -C spm-revision add .
git -C spm-revision commit -qm "first"
first_revision="$(git -C spm-revision rev-parse HEAD)"
short_revision="${first_revision:0:12}"
echo 'print("second commit")' >spm-revision/Sources/revision-tool/main.swift
git -C spm-revision add .
git -C spm-revision commit -qm "second"
git_config="$PWD/spm-gitconfig"
git config --file "$git_config" \
  url."file://$PWD/spm-revision".insteadOf \
  "https://github.com/mise-test/spm-revision.git"

assert \
  "GIT_CONFIG_GLOBAL=$git_config MISE_GIX=0 mise x spm:mise-test/spm-revision@rev:$first_revision -- revision-tool" \
  "first commit"
assert \
  "GIT_CONFIG_GLOBAL=$git_config MISE_GIX=0 mise x spm:mise-test/spm-revision@ref:$short_revision -- revision-tool" \
  "first commit"
assert_fail \
  "GIT_CONFIG_GLOBAL=$git_config MISE_GIX=0 mise install spm:mise-test/spm-revision@rev:deadbeef"
assert_fail "mise where spm:mise-test/spm-revision@rev:deadbeef"

# test package with resources (`templates list` command depends on resources being installed)
#assert "mise x spm:SwiftGen/SwiftGen@6.6.2 --verbose -- swiftgen templates list --only colors" "colors:
#   - literals-swift4
#   - literals-swift5
#   - swift4
#   - swift5
#---
#You can also specify custom templates by path, using \`templatePath\` instead of \`templateName\`.
#For more information, see the documentation on GitHub or use \`swiftgen template doc\`.
#"

if [[ $(uname -s) == Darwin ]]; then
  # Keep mise-managed Swift active to exercise the installer's xcrun selection.
  assert_matches "mise x -- which swift" '/installs/swift/'
fi
mise install node@24 danger-swift@3.22.1 npm:danger@13.0.10
danger_install_path="$(mise where danger-swift@3.22.1)"
test -x "$danger_install_path/bin/danger-swift"
test -f "$danger_install_path/lib/danger/libDanger.dylib" ||
  test -f "$danger_install_path/lib/danger/libDanger.so"
find "$danger_install_path/lib/danger" -name 'Danger.swiftmodule' -print -quit | grep -q .

cat >Dangerfile.swift <<'EOF'
import Danger
import Foundation

nonisolated(unsafe) let danger = Danger()
danger.fail("mise Danger Swift integration test")
EOF

git init -q -b master
git config user.email e2e@example.com
git config user.name "mise e2e"
git add Dangerfile.swift
git commit -q -m initial
git checkout -q -b test
cp Dangerfile.swift Changed.swift
git add Changed.swift
git commit -q -m change

# Foundation must use the isolated test home on Linux as well as macOS.
export CFFIXED_USER_HOME="$HOME"

danger_output="$(
  mise x node@24 danger-swift@3.22.1 npm:danger@13.0.10 -- danger-swift local 2>&1
)"
assert_contains_text "$danger_output" "Danger: ⅹ Failing the build"
assert_contains_text "$danger_output" "mise Danger Swift integration test"
assert_not_contains_text "$danger_output" "Could not find a libDanger"
