# Advanced Validators and Complex Constraints
#
# This example demonstrates sophisticated validation patterns:
# - Field dependencies (options_from)
# - Smart defaults (environment variables, templates)
# - Uniqueness constraints for arrays
# - Multi-field validation rules
# - Form-level constraints

name = "Advanced Validators & Complex Constraints"
description = "Master complex validation patterns and field dependencies"

# ====================================================================
# SMART DEFAULTS - Environment Variable Injection
# ====================================================================

[[elements]]
name = "current_user"
type = "text"
prompt = "Current user"
help = "Auto-populated from environment, read-only"
default = "{{ env.USER }}"
placeholder = "Auto-detected"

[[elements]]
name = "home_directory"
type = "text"
prompt = "Home directory"
help = "Auto-populated from environment"
default = "{{ env.HOME }}"
placeholder = "Auto-detected"

# ====================================================================
# ARRAY WITH UNIQUENESS CONSTRAINT
# ====================================================================

[[elements]]
name = "team_members"
type = "repeating-group"
prompt = "Team Members"
help = "Add unique team members (no duplicates allowed)"
min_items = 1
max_items = 10
fragment = "fragments/team-member.toml"

# ====================================================================
# FIELD DEPENDENCIES - Dynamic Options
# ====================================================================

[[elements]]
name = "platform"
type = "select"
prompt = "Target Platform"
help = "Select platform first, then languages will be filtered"
required = true
options = [
  { value = "web", label = "Web" },
  { value = "mobile", label = "Mobile" },
  { value = "desktop", label = "Desktop" },
  { value = "embedded", label = "Embedded" },
]

[[elements]]
name = "language"
type = "select"
prompt = "Programming Language"
help = "Options depend on selected platform"
required = true
options_from = "platform"
options = [
  # Web languages
  { value = "javascript", label = "JavaScript (Web)" },
  { value = "typescript", label = "TypeScript (Web)" },
  { value = "python", label = "Python (Web)" },
  { value = "rust", label = "Rust (Web)" },
  # Mobile languages
  { value = "kotlin", label = "Kotlin (Mobile)" },
  { value = "swift", label = "Swift (Mobile)" },
  { value = "dart", label = "Dart (Mobile)" },
  # Desktop languages
  { value = "csharp", label = "C# (Desktop)" },
  { value = "cpp", label = "C++ (Desktop)" },
  # Embedded languages
  { value = "c", label = "C (Embedded)" },
  { value = "assembly", label = "Assembly (Embedded)" },
]

# ====================================================================
# CONDITIONAL VALIDATION - Required based on other fields
# ====================================================================

[[elements]]
name = "use_custom_domain"
type = "confirm"
prompt = "Use custom domain"
help = "Enable to specify a custom domain"
default = "false"

[[elements]]
name = "custom_domain"
type = "text"
prompt = "Custom domain"
help = "Only required if 'Use custom domain' is checked"
when = "use_custom_domain == true"
required = true
placeholder = "example.com"

# ====================================================================
# MULTISELECT WITH CONSTRAINTS - Teams/Permissions
# ====================================================================

[[elements]]
name = "role"
type = "select"
prompt = "User Role"
help = "Determines available permissions"
required = true
options = [
  { value = "admin", label = "Administrator" },
  { value = "editor", label = "Editor" },
  { value = "viewer", label = "Viewer" },
]

[[elements]]
name = "permissions"
type = "multiselect"
prompt = "Permissions (select 1-3)"
help = "Select between 1 and 3 permissions"
min_selected = 1
max_selected = 3
required = true
options = [
  { value = "read", label = "📖 Read" },
  { value = "write", label = "✏️ Write" },
  { value = "delete", label = "🗑️ Delete" },
  { value = "admin", label = "🔑 Admin" },
  { value = "audit", label = "🔍 Audit" },
]

# ====================================================================
# NESTED VALIDATION - Complex Object
# ====================================================================

[[sections]]
title = "Server Configuration"
description = "Configure server details with validation"

[[sections.elements]]
name = "hostname"
type = "text"
prompt = "Hostname"
help = "Server hostname (required)"
required = true
placeholder = "example.com"

[[sections.elements]]
name = "port"
type = "text"
prompt = "Port"
help = "Port number (1024-65535)"
required = true
placeholder = "8080"

[[sections.elements]]
name = "max_connections"
type = "text"
prompt = "Max connections"
help = "Maximum concurrent connections (1-10000)"
placeholder = "1000"

[[sections.elements]]
name = "enable_ssl"
type = "confirm"
prompt = "Enable SSL/TLS"
help = "Require HTTPS connections"
default = "true"

[[sections.elements]]
name = "ssl_certificate_path"
type = "text"
prompt = "SSL certificate path"
help = "Path to SSL certificate (required when SSL enabled)"
when = "enable_ssl == true"
required = true
placeholder = "/etc/ssl/certs/cert.pem"

# ====================================================================
# VALIDATION CONSTRAINTS (Configuration)
# ====================================================================

[constraints]
# Port ranges
min_port = 1024
max_port = 65535
privileged_port_limit = 1024

# Connection limits
min_connections = 1
max_connections = 10000
default_connections = 1000

# Domain validation
domain_min_length = 3
domain_max_length = 253

# Field length constraints
hostname_min = 3
hostname_max = 253

password_min_length = 8
password_min_uppercase = 1
password_min_lowercase = 1
password_min_digits = 1
password_min_special = 0

# Array constraints
max_team_members = 50
max_permissions = 5
max_deployments = 20

# File size constraints (in MB)
max_config_file_size = 50
max_backup_file_size = 1000

# Business logic constraints
max_free_tier_users = 5
max_projects_per_user = 20

[constraints.regex_patterns]
# Email pattern
email = "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"

# Hostname pattern (alphanumeric, hyphens, dots)
hostname = "^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$"

# URL pattern
url = "^https?://(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$"

# Port pattern
port = "^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"

# Version pattern (semver)
version = "^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9]+)?$"
