"""
Blockchain Configuration

Centralized configuration for blockchain integration including:
- RPC endpoints
- Contract addresses
- Oracle node addresses
- Network settings

In production, these would be loaded from environment variables.
"""

from typing import Dict, Any
import os


# Network Configuration
NETWORK = os.getenv("BLOCKCHAIN_NETWORK", "sepolia")  # sepolia, polygon-mumbai, mainnet, polygon

# RPC Endpoints
RPC_URLS = {
    "mainnet": os.getenv("ETHEREUM_MAINNET_RPC", "https://eth-mainnet.alchemyapi.io/v2/YOUR-API-KEY"),
    "sepolia": os.getenv("ETHEREUM_SEPOLIA_RPC", "https://eth-sepolia.alchemyapi.io/v2/YOUR-API-KEY"),
    "polygon": os.getenv("POLYGON_MAINNET_RPC", "https://polygon-mainnet.g.alchemy.com/v2/YOUR-API-KEY"),
    "polygon-mumbai": os.getenv("POLYGON_MUMBAI_RPC", "https://polygon-mumbai.g.alchemy.com/v2/YOUR-API-KEY"),
}

# Contract Addresses (per network)
CONTRACT_ADDRESSES = {
    "mainnet": {
        "bet_escrow": os.getenv("MAINNET_BET_ESCROW_ADDRESS", "0x0000000000000000000000000000000000000000"),
        "bet_oracle": os.getenv("MAINNET_BET_ORACLE_ADDRESS", "0x0000000000000000000000000000000000000000"),
    },
    "sepolia": {
        "bet_escrow": os.getenv("SEPOLIA_BET_ESCROW_ADDRESS", "0x1234567890abcdef1234567890abcdef12345678"),
        "bet_oracle": os.getenv("SEPOLIA_BET_ORACLE_ADDRESS", "0xfedcba0987654321fedcba0987654321fedcba09"),
    },
    "polygon": {
        "bet_escrow": os.getenv("POLYGON_BET_ESCROW_ADDRESS", "0x0000000000000000000000000000000000000000"),
        "bet_oracle": os.getenv("POLYGON_BET_ORACLE_ADDRESS", "0x0000000000000000000000000000000000000000"),
    },
    "polygon-mumbai": {
        "bet_escrow": os.getenv("MUMBAI_BET_ESCROW_ADDRESS", "0xabcdef1234567890abcdef1234567890abcdef12"),
        "bet_oracle": os.getenv("MUMBAI_BET_ORACLE_ADDRESS", "0x234567890abcdef1234567890abcdef123456789"),
    },
}

# Backend Signer Configuration
# IMPORTANT: In production, use a secure key management system (AWS KMS, HashiCorp Vault, etc.)
# Never commit private keys to version control
BACKEND_PRIVATE_KEY = os.getenv("BLOCKCHAIN_BACKEND_PRIVATE_KEY", "")

# Oracle Node Configuration
ORACLE_NODES = {
    "node1": {
        "address": os.getenv("ORACLE_NODE1_ADDRESS", "0xNode1Address..."),
        "endpoint": os.getenv("ORACLE_NODE1_ENDPOINT", "https://oracle1.h2h.com"),
    },
    "node2": {
        "address": os.getenv("ORACLE_NODE2_ADDRESS", "0xNode2Address..."),
        "endpoint": os.getenv("ORACLE_NODE2_ENDPOINT", "https://oracle2.h2h.com"),
    },
    "node3": {
        "address": os.getenv("ORACLE_NODE3_ADDRESS", "0xNode3Address..."),
        "endpoint": os.getenv("ORACLE_NODE3_ENDPOINT", "https://oracle3.h2h.com"),
    },
    "node4": {
        "address": os.getenv("ORACLE_NODE4_ADDRESS", "0xNode4Address..."),
        "endpoint": os.getenv("ORACLE_NODE4_ENDPOINT", "https://oracle4.h2h.com"),
    },
    "node5": {
        "address": os.getenv("ORACLE_NODE5_ADDRESS", "0xNode5Address..."),
        "endpoint": os.getenv("ORACLE_NODE5_ENDPOINT", "https://oracle5.h2h.com"),
    },
}

# Oracle Consensus Configuration
ORACLE_CONSENSUS_THRESHOLD = int(os.getenv("ORACLE_CONSENSUS_THRESHOLD", "3"))  # 3 of 5 nodes must agree
ORACLE_TIMEOUT_SECONDS = int(os.getenv("ORACLE_TIMEOUT_SECONDS", "3600"))  # 1 hour

# Indexer Configuration
INDEXER_POLL_INTERVAL = int(os.getenv("INDEXER_POLL_INTERVAL", "10"))  # seconds
INDEXER_START_BLOCK = int(os.getenv("INDEXER_START_BLOCK", "0"))  # 0 = contract deployment block

# Gas Configuration
GAS_PRICE_MULTIPLIER = float(os.getenv("GAS_PRICE_MULTIPLIER", "1.2"))  # 20% above current gas price
MAX_GAS_PRICE_GWEI = int(os.getenv("MAX_GAS_PRICE_GWEI", "500"))  # Never pay more than 500 gwei

# Transaction Configuration
TRANSACTION_TIMEOUT = int(os.getenv("TRANSACTION_TIMEOUT", "300"))  # 5 minutes
CONFIRMATION_BLOCKS = int(os.getenv("CONFIRMATION_BLOCKS", "2"))  # Wait for 2 block confirmations

# API Endpoints for Oracle Data Sources
API_ENDPOINTS = {
    "espn": os.getenv("ESPN_API_KEY", "https://api.espn.com/v1"),
    "odds_api": os.getenv("ODDS_API_KEY", "https://api.the-odds-api.com/v4"),
    "oscars": os.getenv("OSCARS_API_KEY", "https://api.oscars.com"),
}


def get_blockchain_config(network: str = None) -> Dict[str, Any]:
    """
    Get blockchain configuration for specified network.

    Args:
        network: Network name (mainnet, sepolia, polygon, polygon-mumbai)
                If None, uses NETWORK from environment

    Returns:
        Dict with all blockchain configuration
    """
    if network is None:
        network = NETWORK

    if network not in RPC_URLS:
        raise ValueError(f"Unknown network: {network}")

    return {
        "network": network,
        "rpc_url": RPC_URLS[network],
        "bet_escrow_address": CONTRACT_ADDRESSES[network]["bet_escrow"],
        "bet_oracle_address": CONTRACT_ADDRESSES[network]["bet_oracle"],
        "backend_private_key": BACKEND_PRIVATE_KEY,
        "oracle_nodes": ORACLE_NODES,
        "oracle_consensus_threshold": ORACLE_CONSENSUS_THRESHOLD,
        "oracle_timeout": ORACLE_TIMEOUT_SECONDS,
        "indexer_poll_interval": INDEXER_POLL_INTERVAL,
        "indexer_start_block": INDEXER_START_BLOCK,
        "gas_price_multiplier": GAS_PRICE_MULTIPLIER,
        "max_gas_price_gwei": MAX_GAS_PRICE_GWEI,
        "transaction_timeout": TRANSACTION_TIMEOUT,
        "confirmation_blocks": CONFIRMATION_BLOCKS,
        "api_endpoints": API_ENDPOINTS,
    }


# Network Chain IDs (for frontend)
CHAIN_IDS = {
    "mainnet": 1,
    "sepolia": 11155111,
    "polygon": 137,
    "polygon-mumbai": 80001,
}


# Block Explorer URLs (for frontend links)
BLOCK_EXPLORERS = {
    "mainnet": "https://etherscan.io",
    "sepolia": "https://sepolia.etherscan.io",
    "polygon": "https://polygonscan.com",
    "polygon-mumbai": "https://mumbai.polygonscan.com",
}


# Example .env file content:
"""
# Blockchain Configuration
BLOCKCHAIN_NETWORK=sepolia

# RPC Endpoints (get API keys from Alchemy, Infura, or QuickNode)
ETHEREUM_MAINNET_RPC=https://eth-mainnet.alchemyapi.io/v2/YOUR-API-KEY
ETHEREUM_SEPOLIA_RPC=https://eth-sepolia.alchemyapi.io/v2/YOUR-API-KEY
POLYGON_MAINNET_RPC=https://polygon-mainnet.g.alchemy.com/v2/YOUR-API-KEY
POLYGON_MUMBAI_RPC=https://polygon-mumbai.g.alchemy.com/v2/YOUR-API-KEY

# Contract Addresses (update after deployment)
SEPOLIA_BET_ESCROW_ADDRESS=0x1234567890abcdef1234567890abcdef12345678
SEPOLIA_BET_ORACLE_ADDRESS=0xfedcba0987654321fedcba0987654321fedcba09

# Backend Signer (NEVER commit to git!)
BLOCKCHAIN_BACKEND_PRIVATE_KEY=0xYOUR_PRIVATE_KEY_HERE

# Oracle Nodes
ORACLE_NODE1_ADDRESS=0xNode1Address...
ORACLE_NODE1_ENDPOINT=https://oracle1.h2h.com
ORACLE_CONSENSUS_THRESHOLD=3

# Indexer
INDEXER_START_BLOCK=12345678
INDEXER_POLL_INTERVAL=10

# Gas
GAS_PRICE_MULTIPLIER=1.2
MAX_GAS_PRICE_GWEI=500

# External APIs
ESPN_API_KEY=YOUR_ESPN_API_KEY
ODDS_API_KEY=YOUR_ODDS_API_KEY
"""
