import grpc
import os
from mesh_core import agent_pb2_grpc
from agent_node.config import SERVER_HOST_PORT, TLS_ENABLED, CERT_CA, CERT_CLIENT_CRT, CERT_CLIENT_KEY

def get_secure_stub():
    """Initializes a gRPC channel (Secure or Insecure) and returns the orchestrator stub."""
    
    options = [
        ('grpc.keepalive_time_ms', 10000),      # Send keepalive ping every 10s
        ('grpc.keepalive_timeout_ms', 10000),    # Wait 10s for pong
        ('grpc.keepalive_permit_without_calls', True),
        ('grpc.http2.max_pings_without_data', 0), # Allow infinite pings
        ('grpc.max_receive_message_length', 128 * 1024 * 1024),
        ('grpc.max_send_message_length', 128 * 1024 * 1024),
    ]

    if not TLS_ENABLED:
        print(f"[!] TLS is disabled. Connecting via insecure channel to {SERVER_HOST_PORT}")
        channel = grpc.insecure_channel(SERVER_HOST_PORT, options=options)
        return agent_pb2_grpc.AgentOrchestratorStub(channel), channel

    print(f"[*] Connecting via secure (mTLS) channel to {SERVER_HOST_PORT}")
    try:
        with open(CERT_CLIENT_KEY, 'rb') as f: pkey = f.read()
        with open(CERT_CLIENT_CRT, 'rb') as f: cert = f.read()
        with open(CERT_CA, 'rb') as f: ca = f.read()
        
        creds = grpc.ssl_channel_credentials(ca, pkey, cert)
        channel = grpc.secure_channel(SERVER_HOST_PORT, creds, options=options)
        return agent_pb2_grpc.AgentOrchestratorStub(channel), channel
    except FileNotFoundError as e:
        print(f"[!] mTLS Certificate files not found: {e}. Falling back to standard TLS (Server Verify)...")
        # Fallback to standard TLS (uses system CA roots by default)
        creds = grpc.ssl_channel_credentials()
        channel = grpc.secure_channel(SERVER_HOST_PORT, creds, options=options)
        return agent_pb2_grpc.AgentOrchestratorStub(channel), channel
