# Caddy reverse proxy configuration for Sonr services
{
    # Global options
    admin off
    auto_https off
    # Enable debug mode for development
    debug
}

# Health check endpoint
:80/health {
    respond "OK" 200
}

# REST API proxy (Cosmos SDK)
:80/api/* {
    reverse_proxy snrd:1317
    header Access-Control-Allow-Origin *
    header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    header Access-Control-Allow-Headers "Content-Type, Authorization"
}

# gRPC-Web proxy
:80/grpc/* {
    reverse_proxy h2c://snrd:9090
    header Access-Control-Allow-Origin *
    header Access-Control-Allow-Methods "GET, POST, OPTIONS"
    header Access-Control-Allow-Headers "Content-Type, x-grpc-web, x-user-agent"
}

# JSON-RPC proxy (EVM)
:80/rpc {
    reverse_proxy snrd:8545
    header Access-Control-Allow-Origin *
    header Access-Control-Allow-Methods "POST, OPTIONS"
    header Access-Control-Allow-Headers "Content-Type"
}

# WebSocket JSON-RPC proxy (EVM)
:80/ws {
    reverse_proxy snrd:8546
    header Access-Control-Allow-Origin *
}

# Highway service proxy
:80/highway/* {
    reverse_proxy highway:8090
    header Access-Control-Allow-Origin *
    header Access-Control-Allow-Methods "GET, POST, OPTIONS"
    header Access-Control-Allow-Headers "Content-Type, Authorization"
}

# Production configuration with TLS (when CADDY_DOMAIN is set)
{$CADDY_DOMAIN:localhost} {
    # TLS configuration for production
    @production {
        not host localhost
    }
    
    tls @production {
        # Automatic HTTPS with Let's Encrypt
    }

    # Same proxy rules as above
    handle /health {
        respond "OK" 200
    }

    handle /api/* {
        reverse_proxy snrd:1317
    }

    handle /grpc/* {
        reverse_proxy h2c://snrd:9090
    }

    handle /rpc {
        reverse_proxy snrd:8545
    }

    handle /ws {
        reverse_proxy snrd:8546
    }

    handle /highway/* {
        reverse_proxy highway:8090
    }

    # Default handler
    handle {
        respond "Sonr Network Gateway" 200
    }
}