# Orchestrator and Workflow Management Recipes
# ==============================================
# Task orchestration, workflow management, and batch operations

# ============================================================================
# Orchestrator Status and Health
# ============================================================================

# Show orchestrator status
@orch-status:
    echo "📊 Orchestrator Status"
    echo "======================"
    provisioning orch status

@orch-health:
    #!/usr/bin/env bash
    echo "🏥 Orchestrator Health Check"
    echo "============================="
    provisioning orch status --health

@orch-info:
    echo "ℹ️  Orchestrator Information"
    echo "============================"
    provisioning orch info

# Task Management
# ============================================================================

# List all tasks
@orch-tasks:
    echo "📋 All Tasks"
    echo "============"
    provisioning orch tasks

@orch-tasks-limit LIMIT="20":
    #!/usr/bin/env bash
    echo "📋 Tasks (limit {{LIMIT}})"
    echo "=========================="
    provisioning orch tasks --limit {{LIMIT}}

@orch-tasks-status STATUS:
    #!/usr/bin/env bash
    echo "📋 Tasks with status: {{STATUS}}"
    echo "================================="
    provisioning orch tasks --status {{STATUS}}

@orch-tasks-pending:
    #!/usr/bin/env bash
    echo "⏳ Pending Tasks"
    echo "================"
    provisioning orch tasks --status pending

@orch-tasks-running:
    #!/usr/bin/env bash
    echo "🔄 Running Tasks"
    echo "================"
    provisioning orch tasks --status running

@orch-tasks-completed:
    #!/usr/bin/env bash
    echo "✅ Completed Tasks"
    echo "=================="
    provisioning orch tasks --status completed

@orch-tasks-failed:
    #!/usr/bin/env bash
    echo "❌ Failed Tasks"
    echo "==============="
    provisioning orch tasks --status failed

@orch-task-info TASK_ID:
    echo "📄 Task Details: {{TASK_ID}}"
    echo "============================="
    provisioning orch task {{TASK_ID}}

@orch-task-cancel TASK_ID:
    echo "🚫 Cancelling task: {{TASK_ID}}..."
    provisioning orch task {{TASK_ID}} cancel
    echo "✅ Task cancellation requested"

@orch-task-retry TASK_ID:
    echo "🔄 Retrying task: {{TASK_ID}}..."
    provisioning orch task {{TASK_ID}} retry
    echo "✅ Task retry initiated"

# Workflow Management
# ============================================================================

# List all workflows
@workflow-list:
    echo "📋 All Workflows"
    echo "================"
    provisioning workflow list

@workflow-list-filter STATUS="":
    #!/usr/bin/env bash
    echo "📋 Workflows"
    echo "================================================="
    if [ "{{STATUS}}" != "" ]; then
        provisioning workflow list --status "{{STATUS}}"
    else
        provisioning workflow list
    fi

@workflow-status ID:
    echo "📊 Workflow Status: {{ID}}"
    echo "=========================="
    provisioning workflow status {{ID}}

@workflow-monitor ID:
    echo "👁️  Monitoring workflow: {{ID}}"
    echo "==============================="
    echo "Press Ctrl+C to stop monitoring"
    echo ""
    provisioning workflow monitor {{ID}}

@workflow-stats:
    echo "📈 Workflow Statistics"
    echo "======================"
    provisioning workflow stats

@workflow-logs ID:
    echo "📜 Workflow Logs: {{ID}}"
    echo "========================"
    provisioning workflow logs {{ID}}

@workflow-logs-follow ID:
    echo "📜 Following Workflow Logs: {{ID}}"
    echo "===================================="
    echo "Press Ctrl+C to stop"
    echo ""
    provisioning workflow logs {{ID}} "-follow"

# Workflow Validation
# ============================================================================

# Validate KCL workflow definition
@orch-validate FILE:
    echo "✅ Validating workflow: {{FILE}}..."
    provisioning orch validate {{FILE}}

@orch-validate-strict FILE:
    #!/usr/bin/env bash
    echo "✅ Validating workflow (strict): {{FILE}}..."
    provisioning orch validate {{FILE}} --strict

@workflow-dry-run FILE:
    echo "🔍 Dry-run workflow: {{FILE}}..."
    provisioning workflow dry-run {{FILE}}

# Batch Workflow Operations
# ============================================================================

# Submit batch workflow
@batch-submit FILE:
    echo "📤 Submitting batch workflow: {{FILE}}..."
    provisioning batch submit {{FILE}}
    echo "✅ Batch workflow submitted"

@batch-list:
    echo "📋 Batch Workflows"
    echo "=================="
    provisioning batch list

@batch-list-status STATUS:
    #!/usr/bin/env bash
    echo "📋 Batch Workflows ({{STATUS}})"
    echo "==============================="
    provisioning batch list --status {{STATUS}}

@batch-status ID:
    echo "📊 Batch Workflow Status: {{ID}}"
    echo "================================="
    provisioning batch status {{ID}}

@batch-monitor ID:
    echo "👁️  Monitoring batch workflow: {{ID}}"
    echo "======================================"
    echo "Press Ctrl+C to stop monitoring"
    echo ""
    provisioning batch monitor {{ID}}

@batch-rollback ID:
    echo "⏪ Rolling back batch workflow: {{ID}}..."
    provisioning batch rollback {{ID}}
    echo "✅ Rollback initiated"

@batch-cancel ID:
    echo "🚫 Cancelling batch workflow: {{ID}}..."
    provisioning batch cancel {{ID}}
    echo "✅ Batch workflow cancelled"

@batch-stats:
    echo "📈 Batch Workflow Statistics"
    echo "============================="
    provisioning batch stats

# Workflow Templates
# ============================================================================

# List available workflow templates
@workflow-templates:
    echo "📋 Workflow Templates"
    echo "====================="
    provisioning workflow templates

@workflow-template-info NAME:
    echo "📄 Template: {{NAME}}"
    echo "====================="
    provisioning workflow template {{NAME}}

@workflow-from-template NAME OUTPUT:
    #!/usr/bin/env bash
    echo "📝 Creating workflow from template: {{NAME}}..."
    provisioning workflow template {{NAME}} --output {{OUTPUT}}
    echo "✅ Workflow created: {{OUTPUT}}"

# Cleanup Operations
# ============================================================================

# Cleanup completed workflows
@workflow-cleanup:
    echo "🧹 Cleaning up completed workflows..."
    provisioning workflow cleanup
    echo "✅ Cleanup complete"

@workflow-cleanup-old DAYS="30":
    #!/usr/bin/env bash
    echo "🧹 Cleaning up workflows older than {{DAYS}} days..."
    provisioning workflow cleanup --older-than {{DAYS}}
    echo "✅ Cleanup complete"

@workflow-cleanup-failed:
    #!/usr/bin/env bash
    echo "🧹 Cleaning up failed workflows..."
    provisioning workflow cleanup --status failed
    echo "✅ Failed workflows cleaned up"

@workflow-purge:
    #!/usr/bin/env bash
    echo "⚠️  DANGER: This will delete ALL workflows"
    read -p "Type 'DELETE ALL' to confirm: " CONFIRM
    if [ "$CONFIRM" = "DELETE ALL" ]; then
        provisioning workflow purge
        echo "✅ All workflows purged"
    else
        echo "❌ Cancelled"
    fi

# Advanced Operations
# ============================================================================

# Pause workflow execution
@workflow-pause ID:
    echo "⏸️  Pausing workflow: {{ID}}..."
    provisioning workflow pause {{ID}}
    echo "✅ Workflow paused"

@workflow-resume ID:
    echo "▶️  Resuming workflow: {{ID}}..."
    provisioning workflow resume {{ID}}
    echo "✅ Workflow resumed"

@workflow-export ID OUTPUT:
    #!/usr/bin/env bash
    echo "📤 Exporting workflow: {{ID}}..."
    provisioning workflow export {{ID}} --output {{OUTPUT}}
    echo "✅ Workflow exported: {{OUTPUT}}"

@workflow-import FILE:
    echo "📥 Importing workflow: {{FILE}}..."
    provisioning workflow import {{FILE}}
    echo "✅ Workflow imported"

# Quick Workflows
# ============================================================================

# Quick server creation workflow
@quick-server-create INFRA:
    #!/usr/bin/env bash
    echo "⚡ Quick Server Creation: {{INFRA}}"
    echo "===================================="
    provisioning workflow quick server-create --infra {{INFRA}}

@quick-taskserv-install TASKSERV INFRA:
    #!/usr/bin/env bash
    echo "⚡ Quick Taskserv Install: {{TASKSERV}}"
    echo "========================================"
    provisioning workflow quick taskserv-install {{TASKSERV}} --infra {{INFRA}}

@quick-cluster-deploy CLUSTER INFRA:
    #!/usr/bin/env bash
    echo "⚡ Quick Cluster Deploy: {{CLUSTER}}"
    echo "====================================="
    provisioning workflow quick cluster-deploy {{CLUSTER}} --infra {{INFRA}}

# Troubleshooting
# ============================================================================

# Show orchestrator logs
@orch-logs:
    echo "📜 Orchestrator Logs"
    echo "===================="
    provisioning orch logs

@orch-logs-follow:
    #!/usr/bin/env bash
    echo "📜 Following Orchestrator Logs"
    echo "==============================="
    echo "Press Ctrl+C to stop"
    echo ""
    provisioning orch logs --follow

@orch-config:
    echo "⚙️  Orchestrator Configuration"
    echo "=============================="
    provisioning config get orchestrator

@orch-diagnose:
    #!/usr/bin/env bash
    echo "🔍 Orchestrator Diagnostics"
    echo "==========================="
    echo ""
    echo "1. Health check..."
    provisioning orch status --health
    echo ""
    echo "2. Task queue status..."
    provisioning orch tasks --status pending
    echo ""
    echo "3. Recent failures..."
    provisioning orch tasks --status failed --limit 5
    echo ""
    echo "✅ Diagnostics complete"

@orch-restart:
    echo "🔄 Restarting orchestrator..."
    provisioning orch restart
    echo "✅ Orchestrator restarted"

# Monitoring and Statistics
# ============================================================================

# Show real-time orchestrator metrics
@orch-metrics:
    echo "📊 Orchestrator Metrics"
    echo "======================="
    provisioning orch metrics

@orch-queue-stats:
    echo "📈 Task Queue Statistics"
    echo "========================"
    provisioning orch queue-stats

@workflow-history LIMIT="10":
    #!/usr/bin/env bash
    echo "📜 Workflow History ({{LIMIT}} recent)"
    echo "======================================="
    provisioning workflow history --limit {{LIMIT}}

@orch-performance:
    echo "⚡ Performance Statistics"
    echo "========================="
    provisioning orch performance

# Help
# ============================================================================

# Show orchestrator help
@orch-help:
    echo "🎯 ORCHESTRATOR RECIPES"
    echo "======================="
    echo ""
    echo "📊 STATUS & HEALTH"
    echo "   just orch-status                    - Show orchestrator status"
    echo "   just orch-health                    - Health check"
    echo "   just orch-info                      - Detailed information"
    echo ""
    echo "📋 TASK MANAGEMENT"
    echo "   just orch-tasks                     - List all tasks"
    echo "   just orch-tasks-limit <n>           - List with limit"
    echo "   just orch-tasks-status <status>     - Filter by status"
    echo "   just orch-tasks-pending             - Show pending tasks"
    echo "   just orch-tasks-running             - Show running tasks"
    echo "   just orch-tasks-failed              - Show failed tasks"
    echo "   just orch-task-info <id>            - Task details"
    echo "   just orch-task-cancel <id>          - Cancel task"
    echo "   just orch-task-retry <id>           - Retry failed task"
    echo ""
    echo "🔄 WORKFLOW MANAGEMENT"
    echo "   just workflow-list                  - List all workflows"
    echo "   just workflow-status <id>           - Show workflow status"
    echo "   just workflow-monitor <id>          - Monitor real-time"
    echo "   just workflow-stats                 - Show statistics"
    echo "   just workflow-logs <id>             - Show logs"
    echo "   just workflow-pause <id>            - Pause workflow"
    echo "   just workflow-resume <id>           - Resume workflow"
    echo ""
    echo "✅ VALIDATION"
    echo "   just orch-validate <file>           - Validate KCL workflow"
    echo "   just orch-validate-strict <file>    - Strict validation"
    echo "   just workflow-dry-run <file>        - Simulate execution"
    echo ""
    echo "📦 BATCH OPERATIONS"
    echo "   just batch-submit <file>            - Submit batch workflow"
    echo "   just batch-list                     - List batch workflows"
    echo "   just batch-status <id>              - Show batch status"
    echo "   just batch-monitor <id>             - Monitor batch progress"
    echo "   just batch-rollback <id>            - Rollback batch"
    echo "   just batch-cancel <id>              - Cancel batch"
    echo "   just batch-stats                    - Batch statistics"
    echo ""
    echo "📝 TEMPLATES"
    echo "   just workflow-templates             - List templates"
    echo "   just workflow-template-info <name>  - Show template"
    echo "   just workflow-from-template <n> <o> - Create from template"
    echo ""
    echo "🧹 CLEANUP"
    echo "   just workflow-cleanup               - Clean completed workflows"
    echo "   just workflow-cleanup-old <days>    - Clean old workflows"
    echo "   just workflow-cleanup-failed        - Clean failed workflows"
    echo ""
    echo "⚡ QUICK WORKFLOWS"
    echo "   just quick-server-create <infra>    - Quick server creation"
    echo "   just quick-taskserv-install <t> <i> - Quick taskserv install"
    echo "   just quick-cluster-deploy <c> <i>   - Quick cluster deploy"
    echo ""
    echo "🔧 TROUBLESHOOTING"
    echo "   just orch-logs                      - Show logs"
    echo "   just orch-logs-follow               - Follow logs"
    echo "   just orch-config                    - Show configuration"
    echo "   just orch-diagnose                  - Diagnose issues"
    echo "   just orch-restart                   - Restart orchestrator"
    echo ""
    echo "📈 MONITORING"
    echo "   just orch-metrics                   - Real-time metrics"
    echo "   just orch-queue-stats               - Queue statistics"
    echo "   just workflow-history <n>           - Execution history"
    echo "   just orch-performance               - Performance stats"
    echo ""
    echo "💡 EXAMPLES"
    echo "   # Monitor running tasks"
    echo "   just orch-tasks-running"
    echo ""
    echo "   # Submit and monitor batch workflow"
    echo "   just batch-submit workflows/deploy.k"
    echo "   just batch-monitor <workflow-id>"
    echo ""
    echo "   # Validate before submission"
    echo "   just orch-validate workflows/production.k"
    echo ""
    echo "   # Cleanup old workflows"
    echo "   just workflow-cleanup-old 7"
