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

/**
 * @title AddressMapper
 * @notice Maps reserved genesis.json addresses to actual deployed addresses
 * @dev This contract provides a centralized mapping for addresses that were
 *      reserved in genesis.json but deployed to different addresses
 */
contract AddressMapper {
    // Mapping from genesis address to deployed address
    mapping(address => address) private _addressMap;
    
    // Reverse mapping for verification
    mapping(address => address) private _reverseMap;
    
    // Owner for updates
    address public owner;
    
    event AddressMapped(address indexed genesisAddress, address indexed deployedAddress);
    event MappingRemoved(address indexed genesisAddress);
    
    modifier onlyOwner() {
        require(msg.sender == owner, "AddressMapper: caller is not owner");
        _;
    }
    
    constructor() {
        owner = msg.sender;
        
        // Initialize with known mappings
        // WETH9: Genesis -> Deployed
        address weth9Genesis = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
        address weth9Deployed = 0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6;
        _addressMap[weth9Genesis] = weth9Deployed;
        _reverseMap[weth9Deployed] = weth9Genesis;
        emit AddressMapped(weth9Genesis, weth9Deployed);
        
        // WETH10: Genesis -> Deployed
        address weth10Genesis = 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F;
        address weth10Deployed = 0x105F8A15b819948a89153505762444Ee9f324684;
        _addressMap[weth10Genesis] = weth10Deployed;
        _reverseMap[weth10Deployed] = weth10Genesis;
        emit AddressMapped(weth10Genesis, weth10Deployed);
    }
    
    /**
     * @notice Get the deployed address for a genesis address
     * @param genesisAddress The address from genesis.json
     * @return deployedAddress The actual deployed address, or address(0) if not mapped
     */
    function getDeployedAddress(address genesisAddress) external view returns (address) {
        address deployed = _addressMap[genesisAddress];
        // If not mapped, return the genesis address itself (no mapping needed)
        return deployed == address(0) ? genesisAddress : deployed;
    }
    
    /**
     * @notice Get the genesis address for a deployed address
     * @param deployedAddress The deployed address
     * @return genesisAddress The genesis address, or address(0) if not mapped
     */
    function getGenesisAddress(address deployedAddress) external view returns (address) {
        return _reverseMap[deployedAddress];
    }
    
    /**
     * @notice Check if an address is a genesis address that has been mapped
     * @param addr Address to check
     * @return isMapped True if the address has a mapping
     */
    function isMapped(address addr) external view returns (bool) {
        return _addressMap[addr] != address(0);
    }
    
    /**
     * @notice Add or update a mapping (owner only)
     * @param genesisAddress The genesis address
     * @param deployedAddress The deployed address
     */
    function setMapping(address genesisAddress, address deployedAddress) external onlyOwner {
        require(genesisAddress != address(0), "AddressMapper: genesis address cannot be zero");
        require(deployedAddress != address(0), "AddressMapper: deployed address cannot be zero");
        require(genesisAddress != deployedAddress, "AddressMapper: addresses must be different");
        
        // Remove old reverse mapping if exists
        address oldDeployed = _addressMap[genesisAddress];
        if (oldDeployed != address(0)) {
            delete _reverseMap[oldDeployed];
        }
        
        // Remove old forward mapping if deployed address was mapped to something else
        address oldGenesis = _reverseMap[deployedAddress];
        if (oldGenesis != address(0)) {
            delete _addressMap[oldGenesis];
        }
        
        // Set new mappings
        _addressMap[genesisAddress] = deployedAddress;
        _reverseMap[deployedAddress] = genesisAddress;
        
        emit AddressMapped(genesisAddress, deployedAddress);
    }
    
    /**
     * @notice Remove a mapping (owner only)
     * @param genesisAddress The genesis address to remove
     */
    function removeMapping(address genesisAddress) external onlyOwner {
        address deployed = _addressMap[genesisAddress];
        require(deployed != address(0), "AddressMapper: mapping does not exist");
        
        delete _addressMap[genesisAddress];
        delete _reverseMap[deployed];
        
        emit MappingRemoved(genesisAddress);
    }
    
    /**
     * @notice Transfer ownership (owner only)
     * @param newOwner The new owner address
     */
    function transferOwnership(address newOwner) external onlyOwner {
        require(newOwner != address(0), "AddressMapper: new owner cannot be zero");
        owner = newOwner;
    }
}

