#!/usr/bin/env bash

# Test monorepo task wildcards with ellipsis for paths and asterisk for task names
export MISE_EXPERIMENTAL=1

# Create monorepo root config
cat <<EOF >mise.toml
experimental_monorepo_root = true

[tasks.root-task]
run = 'echo "root task"'
EOF

# Create project structure with nested tasks
mkdir -p projects/frontend
cat <<EOF >projects/frontend/mise.toml
[tasks.build]
run = 'echo "frontend build"'

[tasks.test]
run = 'echo "frontend test"'

[tasks."test:unit"]
run = 'echo "frontend test:unit"'

[tasks."test:integration"]
run = 'echo "frontend test:integration"'
EOF

mkdir -p projects/backend
cat <<EOF >projects/backend/mise.toml
[tasks.build]
run = 'echo "backend build"'

[tasks.test]
run = 'echo "backend test"'

[tasks.deploy]
run = 'echo "backend deploy"'
EOF

mkdir -p libs/shared
cat <<EOF >libs/shared/mise.toml
[tasks.build]
run = 'echo "shared build"'

[tasks.lint]
run = 'echo "shared lint"'
EOF

# Verify monorepo tasks are discovered with --all flag
assert_contains "mise tasks ls --all" "//projects/frontend:build"
assert_contains "mise tasks ls --all" "//projects/backend:build"
assert_contains "mise tasks ls --all" "//libs/shared:build"

# By default (without --all), only shows tasks from current hierarchy
# At root, this includes the root task (shown as //:root-task)
assert_contains "mise tasks ls" "//:root-task"
assert_not_contains "mise tasks ls" "//projects/frontend:build"
assert_not_contains "mise tasks ls" "//projects/backend:build"
assert_not_contains "mise tasks ls" "//libs/shared:build"

# Test from subdirectory - shows local tasks with full monorepo paths
cd projects/frontend
assert_contains "mise tasks ls" "//:root-task"
assert_contains "mise tasks ls" "//projects/frontend:build"
assert_contains "mise tasks ls" "//projects/frontend:test"
assert_not_contains "mise tasks ls" "//projects/backend:build"
assert_not_contains "mise tasks ls" "//libs/shared:build"

# From subdirectory with --all - should see everything
assert_contains "mise tasks ls --all" "//projects/frontend:build"
assert_contains "mise tasks ls --all" "//projects/backend:build"
assert_contains "mise tasks ls --all" "//libs/shared:build"

# Go back to root
cd ../..

# Test ellipsis for path matching - match all build tasks
assert_contains "mise run '//...:build'" "frontend build"
assert_contains "mise run '//...:build'" "backend build"
assert_contains "mise run '//...:build'" "shared build"

# Test ellipsis with path prefix - match all test tasks under projects/
assert_contains "mise run '//projects/...:test'" "frontend test"
assert_contains "mise run '//projects/...:test'" "backend test"
assert_not_contains "mise run '//projects/...:test'" "shared"

# Test asterisk for task name matching - all tasks in backend
assert_contains "mise run '//projects/backend:*'" "backend build"
assert_contains "mise run '//projects/backend:*'" "backend test"
assert_contains "mise run '//projects/backend:*'" "backend deploy"

# Test asterisk with prefix - match tasks starting with test:
assert_contains "mise run '//projects/frontend:test:*'" "frontend test:unit"
assert_contains "mise run '//projects/frontend:test:*'" "frontend test:integration"
assert_not_contains "mise run '//projects/frontend:test:*'" "frontend build"

# Test that relative patterns (without //) are not supported
assert_fail "mise run 'projects/...:build'" "relative path syntax"

# Test combined patterns - ellipsis in path, asterisk in task
assert_contains "mise run '//projects/...:test*'" "frontend test"
assert_contains "mise run '//projects/...:test*'" "frontend test:unit"
assert_contains "mise run '//projects/...:test*'" "frontend test:integration"
assert_contains "mise run '//projects/...:test*'" "backend test"

# Test ellipsis wildcard in different positions
# Create test structure with nested directories
mkdir -p projects/services/api/v1
cat <<SUBEOF >projects/services/api/v1/mise.toml
[tasks.deploy]
run = 'echo "api v1 deploy"'
SUBEOF

mkdir -p projects/tools/cli
cat <<SUBEOF >projects/tools/cli/mise.toml
[tasks.build]
run = 'echo "cli build"'
SUBEOF

mkdir -p libs/shared/utils
cat <<SUBEOF >libs/shared/utils/mise.toml
[tasks.test]
run = 'echo "utils test"'
SUBEOF

# Test 1: ... at the START of path (matches any depth before)
# Pattern: //.../cli:build should match //projects/tools/cli:build
assert_contains "mise run '//.../cli:build'" "cli build"

# Test 2: ... in the MIDDLE of path
# Pattern: //projects/.../api:task should match //projects/services/api/v1:deploy
assert_contains "mise run '//projects/.../v1:deploy'" "api v1 deploy"

# Pattern: //libs/.../utils:test should match //libs/shared/utils:test
assert_contains "mise run '//libs/.../utils:test'" "utils test"

# Test 3: ... at the END of path (matches all subdirectories)
# Pattern: //projects/tools/...:build should match //projects/tools/cli:build
assert_contains "mise run '//projects/tools/...:build'" "cli build"

# Pattern: //projects/...:deploy should match //projects/services/api/v1:deploy
assert_contains "mise run '//projects/...:deploy'" "api v1 deploy"

# Test 4: Multiple ... in one pattern
# Pattern: //projects/.../api/...:deploy should match //projects/services/api/v1:deploy
assert_contains "mise run '//projects/.../api/...:deploy'" "api v1 deploy"

# Test 5: ... alone (matches everything)
assert_contains "mise run '//...:build'" "cli build"
assert_contains "mise run '//...:test'" "utils test"

# Verify tasks are discovered
assert_contains "mise tasks --all" "//projects/services/api/v1:deploy"
assert_contains "mise tasks --all" "//projects/tools/cli:build"
assert_contains "mise tasks --all" "//libs/shared/utils:test"
