#!/usr/bin/env bash

# Test that task edit with nested colons (foo:bar:baz) creates the correct file structure
# and can be run with `mise run foo:bar:baz`

# Create a temporary editor script that just writes a simple task
export EDITOR="$PWD/fake-editor"
cat <<'EDITOR_EOF' >fake-editor
#!/usr/bin/env bash
# Don't actually open an editor, just ensure the file has some content
if [ ! -s "$1" ]; then
	cat >"$1" <<'EOF'
#!/usr/bin/env bash
set -euxo pipefail

echo "nested task executed"
EOF
fi
EDITOR_EOF
chmod +x fake-editor

# Test 1: Create a task with nested colons using task edit
echo "Running: mise tasks edit foo:bar:baz --path"
TASK_PATH=$(mise tasks edit foo:bar:baz --path 2>&1)

# Verify the file was created
if [ ! -f "$TASK_PATH" ]; then
  echo "ERROR: Task file was not created at $TASK_PATH"
  exit 1
fi

echo "Task file created at: $TASK_PATH"

# Write content to the task file
cat >"$TASK_PATH" <<'EOF'
#!/usr/bin/env bash
set -euxo pipefail

echo "nested task executed"
EOF
chmod +x "$TASK_PATH"

# The key test: Can we run the task with its nested name?
# Should successfully execute and output our message
assert_contains "mise run foo:bar:baz" "nested task executed"

# Test 2: Verify the task shows up in task list with the correct name
assert_contains "mise tasks ls" "foo:bar:baz"

# Test 3: Try editing an existing nested task (should not create duplicates)
TASK_PATH2=$(mise tasks edit foo:bar:baz --path 2>&1)

if [ "$TASK_PATH" != "$TASK_PATH2" ]; then
  echo "ERROR: Editing the same task twice gave different paths"
  echo "First edit: $TASK_PATH"
  echo "Second edit: $TASK_PATH2"
  exit 1
fi

# Test 4: Create a task with multiple levels of nesting
TASK_PATH3=$(mise tasks edit deploy:prod:backend:api --path 2>&1)

if [ ! -f "$TASK_PATH3" ]; then
  echo "ERROR: Deeply nested task file was not created at $TASK_PATH3"
  exit 1
fi

echo "Deeply nested task file created at: $TASK_PATH3"

# Write content to the deeply nested task
cat >"$TASK_PATH3" <<'EOF'
#!/usr/bin/env bash
set -euxo pipefail

echo "deeply nested task executed"
EOF
chmod +x "$TASK_PATH3"

# Run the deeply nested task
assert_contains "mise run deploy:prod:backend:api" "deeply nested task executed"

# Verify it shows up in the task list
assert_contains "mise tasks ls" "deploy:prod:backend:api"

# Default includes select the first directory that already exists.
mkdir -p default-project/.mise-tasks
(
  cd default-project
  DEFAULT_PATH=$(mise tasks edit default-choice --path 2>&1)
  assert "printf '%s' \"$DEFAULT_PATH\"" "$PWD/.mise-tasks/default-choice"
)

# A missing explicit path is not assumed to be a directory and never falls back to a default.
cat <<'EOF' >mise.toml
[task_config]
includes = ["tasks"]
EOF

assert_fail_contains "mise tasks edit configured --path" "do not contain an existing directory"
assert_fail_contains "mise tasks add added --file" "do not contain an existing directory"
assert_fail "test -e tasks/configured"
assert_fail "test -e tasks/added"
assert_fail "test -e mise-tasks/configured"
assert_fail "test -e mise-tasks/added"

# Select the first existing directory in include order.
mkdir existing-tasks
cat <<'EOF' >mise.toml
[task_config]
includes = ["tasks.d", "existing-tasks"]
EOF

TASK_PATH5=$(mise tasks edit dotted --path 2>&1)
assert "printf '%s' \"$TASK_PATH5\"" "$PWD/existing-tasks/dotted"
assert "test -f \"$TASK_PATH5\""

# Multiple missing literal directories are ambiguous and must not be guessed.
cat <<'EOF' >mise.toml
[task_config]
includes = ["missing-a", "missing-b"]
EOF

assert_fail_contains "mise tasks edit ambiguous --path" "do not contain an existing directory"
assert_fail "test -e missing-a/ambiguous"
assert_fail "test -e missing-b/ambiguous"

# Explicit includes replace the default directories; do not fall back to mise-tasks.
cat <<'EOF' >tasks.toml
[existing]
run = "echo existing"
EOF
cat <<'EOF' >mise.toml
[task_config]
includes = ["tasks.toml"]
EOF

assert "mise tasks edit existing --path" "$PWD/tasks.toml"
assert_fail_contains "mise tasks edit no-directory --path" "do not contain an existing directory"
assert_fail "test -e mise-tasks/no-directory"

# Existing inline tasks also remain editable without a file-task directory.
cat <<'EOF' >mise.toml
[task_config]
includes = []

[tasks.inline]
run = "echo inline"
EOF

assert "mise tasks edit inline --path" "$PWD/mise.toml"
