// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Script, console} from "forge-std/Script.sol";
import {ChainRegistry} from "../../contracts/registry/ChainRegistry.sol";

/**
 * @title RegisterAllMainnet
 * @notice Register ALL Mainnet (651940) in ChainRegistry
 * @dev Run this after deploying AlltraAdapter
 */
contract RegisterAllMainnet is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        // Load environment variables
        address registryAddress = vm.envOr("CHAIN_REGISTRY_ADDRESS", address(0));
        address alltraAdapterAddress = vm.envOr("ALLTRA_ADAPTER_ADDRESS", address(0));
        
        require(registryAddress != address(0), "CHAIN_REGISTRY_ADDRESS not set");
        require(alltraAdapterAddress != address(0), "ALLTRA_ADAPTER_ADDRESS not set");
        
        console.log("Registering ALL Mainnet (651940) in ChainRegistry");
        console.log("Registry:", vm.toString(registryAddress));
        console.log("Adapter:", vm.toString(alltraAdapterAddress));
        
        vm.startBroadcast(deployerPrivateKey);
        
        ChainRegistry registry = ChainRegistry(registryAddress);
        
        // Register ALL Mainnet as EVM chain
        // Parameters:
        // - chainId: 651940
        // - adapter: AlltraAdapter address
        // - explorerUrl: https://alltra.global
        // - minConfirmations: 12 (standard EVM)
        // - avgBlockTime: 2 seconds (TBD - verify actual block time)
        // - additionalData: empty (can include CCIP selector if available later)
        registry.registerEVMChain(
            651940,  // chainId
            alltraAdapterAddress,  // adapter
            "https://alltra.global",  // explorerUrl
            12,  // minConfirmations
            2,  // avgBlockTime (seconds) - TODO: Verify actual block time
            ""  // additionalData
        );
        
        vm.stopBroadcast();
        
        console.log("\n=== Registration Summary ===");
        console.log("Chain ID: 651940");
        console.log("Chain Name: ALL Mainnet");
        console.log("Adapter:", vm.toString(alltraAdapterAddress));
        console.log("Explorer: https://alltra.global");
        console.log("Min Confirmations: 12");
        console.log("Avg Block Time: 2 seconds");
        console.log("\n✅ ALL Mainnet registered successfully!");
        console.log("\nNext Steps:");
        console.log("1. Verify chain registration: cast call <REGISTRY> 'evmChains(uint256)(uint256,string,uint8,address,bool,uint256,uint256,bool,string,string,bytes)' 651940 --rpc-url <RPC>");
        console.log("2. Test adapter: Verify AlltraAdapter.getChainIdentifier() returns (651940, 'ALL-Mainnet')");
        console.log("3. Update routing services to use AlltraAdapter for chain 651940");
    }
}
