# Sample YAML File: Anchors and Aliases
# Provided by Sample-Files.com
# Demonstrates DRY patterns using &anchors, *aliases, and <<merge keys

# Define reusable defaults using anchors
defaults:
  database: &db_defaults
    driver: postgresql
    port: 5432
    pool_min: 5
    pool_max: 20
    timeout: 30
    ssl: true
    options:
      connect_timeout: 10
      application_name: sampleapp
      statement_timeout: 30000

  logging: &log_defaults
    format: json
    level: INFO
    max_file_size: 104857600  # 100 MB
    max_files: 10
    compress_rotated: true

  retry: &retry_defaults
    max_retries: 3
    initial_delay: 1
    max_delay: 30
    backoff_multiplier: 2

  health_check: &health_defaults
    interval: 30
    timeout: 10
    retries: 3
    start_period: 60

# Use anchors in environment-specific configs
environments:
  development:
    database:
      <<: *db_defaults         # Merge all defaults
      host: localhost           # Override specific values
      name: sampleapp_dev
      username: dev_user
      password: dev_password
      ssl: false                # Override: no SSL in dev
      pool_min: 2
      pool_max: 5
    logging:
      <<: *log_defaults
      level: DEBUG              # Override: verbose in dev
      output: console
    retry: *retry_defaults      # Use defaults as-is (alias, not merge)
    health_check: *health_defaults

  staging:
    database:
      <<: *db_defaults
      host: staging-db.example.com
      name: sampleapp_staging
      username: staging_user
      password: "${STAGING_DB_PASSWORD}"
    logging:
      <<: *log_defaults
      level: DEBUG
      output: file
    retry:
      <<: *retry_defaults
      max_retries: 5            # Override: more retries in staging
    health_check: *health_defaults

  production:
    database:
      <<: *db_defaults
      host: prod-db.example.com
      name: sampleapp_prod
      username: prod_user
      password: "${PROD_DB_PASSWORD}"
      pool_min: 10
      pool_max: 50
    logging:
      <<: *log_defaults
      level: WARN               # Override: less verbose in prod
      output: file
    retry:
      <<: *retry_defaults
      max_retries: 5
      max_delay: 60
    health_check:
      <<: *health_defaults
      interval: 15              # Override: more frequent in prod
      retries: 5

# Anchor reuse for service definitions
services:
  - &web_service
    name: web
    replicas: 3
    port: 8080
    health_check: *health_defaults
    retry: *retry_defaults

  - &worker_service
    name: worker
    replicas: 2
    port: null
    health_check: *health_defaults
    retry:
      <<: *retry_defaults
      max_retries: 10
      max_delay: 120

  - <<: *web_service
    name: api
    replicas: 5
    port: 8081

  - <<: *worker_service
    name: scheduler
    replicas: 1
