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

import {Script, console} from "forge-std/Script.sol";
import {DODOPMMProvider} from "../../contracts/liquidity/providers/DODOPMMProvider.sol";
import {DODOPMMIntegration} from "../../contracts/dex/DODOPMMIntegration.sol";

/**
 * @title RegisterDODOPools
 * @notice Register all existing DODO PMM pools from DODOPMMIntegration with DODOPMMProvider.
 * @dev Set DODO_PMM_PROVIDER_ADDRESS, DODO_PMM_INTEGRATION (or DODO_PMM_INTEGRATION_ADDRESS).
 *      Reads integration.getAllPools() and poolConfigs(pool), then registers both directions
 *      for every discovered pool so provider.supportsTokenPair() and executeSwap() work
 *      symmetrically across the current live set and any future c* full-mesh expansion.
 */
contract RegisterDODOPools is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address providerAddr = vm.envAddress("DODO_PMM_PROVIDER_ADDRESS");
        address integrationAddr = vm.envAddress("DODO_PMM_INTEGRATION");
        if (integrationAddr == address(0)) integrationAddr = vm.envAddress("DODO_PMM_INTEGRATION_ADDRESS");
        require(providerAddr != address(0), "DODO_PMM_PROVIDER_ADDRESS not set");
        require(integrationAddr != address(0), "DODO_PMM_INTEGRATION not set");

        DODOPMMIntegration integration = DODOPMMIntegration(integrationAddr);
        DODOPMMProvider provider = DODOPMMProvider(providerAddr);
        address[] memory pools = integration.getAllPools();
        require(pools.length > 0, "No pools found in DODOPMMIntegration");

        vm.startBroadcast(pk);
        for (uint256 i = 0; i < pools.length; i++) {
            try integration.getPoolConfig(pools[i]) returns (DODOPMMIntegration.PoolConfig memory config) {
                _registerPair(provider, config.baseToken, config.quoteToken, pools[i]);
            } catch {
                console.log("Skipping removed or unreadable pool:", pools[i]);
            }
        }
        vm.stopBroadcast();
    }

    function _registerPair(
        DODOPMMProvider provider,
        address tokenA,
        address tokenB,
        address pool
    ) internal {
        provider.registerPool(tokenA, tokenB, pool);
        provider.registerPool(tokenB, tokenA, pool);
        console.log("Registered base:", tokenA);
        console.log("Registered quote:", tokenB);
        console.log("Pool:", pool);
    }
}
