# Info: KCL Polkadot Bootnode task schemas for provisioning (Provisioning)
# Author: Provisioning System
# Release: 0.0.1
# Date: 2025-07-24

import regex

schema User:
    """
    User settings for Polkadot bootnode
    """
    name: str
    group: str = name
    home?: str = "/home/${name}"

schema SSL:
    """
    SSL certificate configuration for bootnode
    """
    enabled: bool = False
    cert_file?: str
    key_file?: str
    ca_file?: str
    
    check:
        not enabled or cert_file != Undefined and len(cert_file) > 0, "cert_file required when SSL enabled"
        not enabled or key_file != Undefined and len(key_file) > 0, "key_file required when SSL enabled"

schema NetworkPorts:
    """
    Network port configuration for bootnode
    """
    p2p_port: int = 30310
    ws_port: int = 30311
    wss_port: int = 30312
    
    check:
        1 <= p2p_port <= 65535, "P2P port must be between 1 and 65535"
        1 <= ws_port <= 65535, "WebSocket port must be between 1 and 65535"
        1 <= wss_port <= 65535, "WSS port must be between 1 and 65535"
        p2p_port != ws_port, "P2P and WebSocket ports must be different"
        p2p_port != wss_port, "P2P and WSS ports must be different"
        ws_port != wss_port, "WebSocket and WSS ports must be different"

schema WSS:
    """
    Secure WebSocket configuration for bootnode
    """
    enabled: bool = False
    domain?: str
    proxy_type: "nginx" | "apache" = "nginx"
    rate_limit: int = 1000  # requests per minute
    ssl: SSL = {}
    
    check:
        not enabled or ssl.enabled, "SSL must be enabled for WSS"
        not enabled or domain != Undefined and len(domain) > 0, "domain required for WSS"

schema Network:
    """
    Network configuration for bootnode
    """
    chain: "polkadot" | "kusama" | "westend" = "polkadot"
    listen_addrs: [str] = [
        "/ip4/0.0.0.0/tcp/30310",
        "/ip4/0.0.0.0/tcp/30311/ws"
    ]
    public_addr?: str
    external_addresses: [str] = []
    max_peers: int = 50
    ports: NetworkPorts = {}
    
    check:
        max_peers > 0, "max_peers must be positive"
        len(listen_addrs) > 0, "At least one listen address required"

schema Telemetry:
    """
    Telemetry configuration for bootnode
    """
    enabled: bool = True
    url: str = "wss://telemetry.polkadot.io/submit/"
    verbosity: int = 0
    
    check:
        0 <= verbosity <= 9, "verbosity must be between 0 and 9"

schema PolkadotBootnode:
    """
    Polkadot bootnode configuration
    """
    name: str = "polkadot-bootnode"
    version: str = "latest"
    run_user: User = {
        name = "polkadot"
    }
    work_path: str = "/var/lib/polkadot-bootnode"
    config_path: str = "/etc/polkadot-bootnode"
    bin_path: str = "/usr/local/bin/polkadot"
    base_path: str = "/var/lib/polkadot-bootnode/data"
    node_key_file?: str = "/etc/polkadot-bootnode/node-key"
    network: Network = {}
    telemetry: Telemetry = {}
    wss: WSS = {}
    log_level: "error" | "warn" | "info" | "debug" | "trace" = "info"
    log_targets: [str] = ["sub-libp2p"]
    execution: "native" | "wasm" | "both" = "wasm"
    state_cache_size: int = 67108864  # 64MB
    
    check:
        len(run_user.name) > 0, "Check run_user name"
        len(work_path) > 0, "Check work_path"
        len(config_path) > 0, "Check config_path"
        len(base_path) > 0, "Check base_path"
        state_cache_size > 0, "state_cache_size must be positive"