# | Batch workflow examples for provisioning
# | Migrated from: provisioning/kcl/examples_batch.k
# | Pattern: Three-file (contracts + defaults + instances)

let workflows = import "../workflows/main.ncl" in
let batch = import "./main.ncl" in
let settings = import "../../config/settings/main.ncl" in
let defaults = workflows.defaults in

{
  # Example 1: Mixed Provider Infrastructure Deployment
  mixed_provider_workflow = workflows.make_batch_workflow {
    workflow_id = "mixed_infra_deploy_001",
    name = "Mixed Provider Infrastructure Deployment",
    description = "Deploy infrastructure across UpCloud and AWS with cross-provider networking",
    operations = [
      workflows.make_batch_operation {
        operation_id = "create_upcloud_servers",
        name = "Create UpCloud Web Servers",
        operation_type = "server",
        provider = "upcloud",
        action = "create",
        parameters = {
          "server_count" = "3",
          "server_type" = "web",
          "zone" = "fi-hel2",
          "plan" = "1xCPU-2GB",
        },
        allow_parallel = true,
        priority = 10,
      },
      workflows.make_batch_operation {
        operation_id = "create_aws_database",
        name = "Create AWS RDS Database",
        operation_type = "server",
        provider = "aws",
        action = "create",
        parameters = {
          "service" = "rds",
          "instance_class" = "db.t3.micro",
          "engine" = "postgresql",
          "region" = "eu-west-1",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "create_upcloud_servers",
            dependency_type = "sequential",
            timeout = 600,
          },
        ],
        priority = 5,
      },
      workflows.make_batch_operation {
        operation_id = "install_kubernetes",
        name = "Install Kubernetes on UpCloud servers",
        operation_type = "taskserv",
        provider = "upcloud",
        action = "create",
        parameters = {
          "taskserv" = "kubernetes",
          "version" = "v1.28.0",
          "cluster_name" = "prod-cluster",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "create_upcloud_servers",
            dependency_type = "sequential",
            timeout = 1200,
          },
        ],
        timeout = 3600,
        priority = 8,
      },
      workflows.make_batch_operation {
        operation_id = "setup_monitoring",
        name = "Setup Prometheus monitoring",
        operation_type = "taskserv",
        action = "create",
        parameters = {
          "taskserv" = "prometheus",
          "namespace" = "monitoring",
          "retention" = "30d",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "install_kubernetes",
            dependency_type = "sequential",
            timeout = 600,
          },
        ],
        priority = 3,
      },
    ],
    max_parallel_operations = 3,
    fail_fast = false,
    storage = workflows.make_storage_config {
      backend = "surrealdb",
      connection_config = {
        "url" = "ws://localhost:8000",
        "namespace" = "provisioning",
        "database" = "batch_workflows",
      },
      enable_persistence = true,
      retention_hours = 720,
    },
    monitoring = workflows.make_monitoring_config {
      enabled = true,
      backend = "prometheus",
      enable_tracing = true,
      enable_notifications = true,
      notification_channels = ["webhook:slack://ops-channel"],
    },
    default_retry_policy = workflows.make_retry_policy {
      max_attempts = 3,
      initial_delay = 10,
      backoff_multiplier = 2,
      retry_on_errors = ["connection_error", "timeout", "rate_limit", "resource_unavailable"],
    },
    execution_context = {
      "environment" = "production",
      "cost_center" = "infrastructure",
      "owner" = "devops-team",
    },
  },

  # Example 2: Server Scaling Workflow with SurrealDB Backend
  server_scaling_workflow = workflows.make_batch_workflow {
    workflow_id = "server_scaling_002",
    name = "Auto-scaling Server Workflow",
    description = "Scale servers based on load with automatic rollback on failure",
    operations = [
      workflows.make_batch_operation {
        operation_id = "scale_web_servers",
        name = "Scale web servers up",
        operation_type = "server",
        action = "scale",
        parameters = {
          "target_count" = "6",
          "current_count" = "3",
          "server_group" = "web-tier",
        },
        retry_policy = workflows.make_retry_policy {
          max_attempts = 2,
          initial_delay = 30,
          retry_on_errors = ["resource_limit", "quota_exceeded"],
        },
        rollback_strategy = workflows.make_rollback_strategy {
          enabled = true,
          strategy = "immediate",
          custom_rollback_operations = ["scale_down_to_original"],
        },
      },
      workflows.make_batch_operation {
        operation_id = "update_load_balancer",
        name = "Update load balancer configuration",
        operation_type = "custom",
        action = "configure",
        parameters = {
          "service" = "haproxy",
          "config_template" = "web_tier_6_servers",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "scale_web_servers",
            dependency_type = "conditional",
            conditions = ["servers_ready", "health_check_passed"],
            timeout = 300,
          },
        ],
      },
    ],
    storage = workflows.make_storage_config {
      backend = "surrealdb",
      connection_config = {
        "url" = "ws://surrealdb.local:8000",
        "namespace" = "scaling",
        "database" = "operations",
      },
    },
    fail_fast = true,
  },

  # Example 3: Maintenance Workflow with Filesystem Backend
  maintenance_workflow = workflows.make_batch_workflow {
    workflow_id = "maintenance_003",
    name = "System Maintenance Workflow",
    description = "Perform scheduled maintenance across multiple providers",
    operations = [
      workflows.make_batch_operation {
        operation_id = "backup_databases",
        name = "Backup all databases",
        operation_type = "custom",
        action = "create",
        parameters = {
          "backup_type" = "full",
          "compression" = "gzip",
          "retention_days" = "30",
        },
        timeout = 7200,
      },
      workflows.make_batch_operation {
        operation_id = "update_taskservs",
        name = "Update all taskservs to latest versions",
        operation_type = "taskserv",
        action = "update",
        parameters = {
          "update_strategy" = "rolling",
          "max_unavailable" = "1",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "backup_databases",
            dependency_type = "sequential",
          },
        ],
        allow_parallel = false,
      },
      workflows.make_batch_operation {
        operation_id = "verify_services",
        name = "Verify all services are healthy",
        operation_type = "custom",
        action = "configure",
        parameters = {
          "verification_type" = "health_check",
          "timeout_per_service" = "30",
        },
        dependencies = [
          workflows.make_dependency_def {
            target_operation_id = "update_taskservs",
            dependency_type = "sequential",
          },
        ],
      },
    ],
    storage = workflows.make_storage_config {
      backend = "filesystem",
      base_path = "./maintenance_workflows",
      enable_persistence = true,
      enable_compression = true,
    },
    pre_workflow_hooks = ["notify_maintenance_start", "set_maintenance_mode"],
    post_workflow_hooks = ["unset_maintenance_mode", "notify_maintenance_complete"],
  },

  # Example 4: Production Batch Executor (uses defaults as foundation)
  production_batch_executor = batch.defaults.executor,

  # Example 5: Template for Common Infrastructure Deployment
  infra_deployment_template = {
    template_id = "standard_infra_deployment",
    name = "Standard Infrastructure Deployment Template",
    description = "Template for deploying standard infrastructure with customizable parameters",
    category = "infrastructure",
    version = defaults.workflow_template.version,
    workflow_template = {
      workflow_id = "custom_deployment",
      name = "Custom Deployment",
      description = defaults.batch_workflow.description,
      version = defaults.batch_workflow.version,
      operations = [
        {
          operation_id = "create_servers",
          name = "Create servers",
          operation_type = "server",
          provider = "upcloud",
          action = "create",
          parameters = {
            "count" = "3",
            "type" = "web",
            "zone" = "fi-hel2",
          },
          dependencies = defaults.batch_operation.dependencies,
          timeout = defaults.batch_operation.timeout,
          allow_parallel = defaults.batch_operation.allow_parallel,
          priority = defaults.batch_operation.priority,
          validation_rules = defaults.batch_operation.validation_rules,
          success_conditions = defaults.batch_operation.success_conditions,
        },
        {
          operation_id = "install_base_taskservs",
          name = "Install base taskservs",
          operation_type = "taskserv",
          action = "create",
          parameters = {
            "taskservs" = "kubernetes,prometheus,grafana",
          },
          dependencies = [
            {
              target_operation_id = "create_servers",
              dependency_type = "sequential",
              conditions = defaults.dependency_def.conditions,
              timeout = defaults.dependency_def.timeout,
              fail_on_dependency_error = defaults.dependency_def.fail_on_dependency_error,
            },
          ],
          timeout = defaults.batch_operation.timeout,
          allow_parallel = defaults.batch_operation.allow_parallel,
          priority = defaults.batch_operation.priority,
          validation_rules = defaults.batch_operation.validation_rules,
          success_conditions = defaults.batch_operation.success_conditions,
        },
      ],
      max_parallel_operations = defaults.batch_workflow.max_parallel_operations,
      global_timeout = defaults.batch_workflow.global_timeout,
      fail_fast = defaults.batch_workflow.fail_fast,
      execution_context = defaults.batch_workflow.execution_context,
      pre_workflow_hooks = defaults.batch_workflow.pre_workflow_hooks,
      post_workflow_hooks = defaults.batch_workflow.post_workflow_hooks,
    },
    parameters = {
      "workflow_id" = "custom_deployment",
      "workflow_name" = "Custom Deployment",
      "server_count" = "3",
      "server_type" = "web",
      "provider" = "upcloud",
      "zone" = "fi-hel2",
      "base_taskservs" = "kubernetes,prometheus,grafana",
      "storage_backend" = "filesystem",
      "storage_path" = "./deployments",
    },
    required_parameters = [
      "workflow_id",
      "server_count",
      "provider",
    ],
    examples = [
      "Small deployment: server_count=2, server_type=micro",
      "Production deployment: server_count=6, server_type=standard, provider=upcloud",
    ],
  },
}
