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

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

/**
 * @title CreatePublicXAUPoolsChain138
 * @notice Create the public XAU-anchored pools on Chain 138 using the configured cXAU anchor token.
 * @dev Env:
 *      - PRIVATE_KEY
 *      - DODOPMM_INTEGRATION or DODOPMM_INTEGRATION_ADDRESS
 *      - XAU_ADDRESS_138 or CXAUC_ADDRESS_138 or CXAUT_ADDRESS_138
 *      Optional:
 *      - COMPLIANT_USDT_ADDRESS, COMPLIANT_USDC_ADDRESS, cEURT_ADDRESS_138
 *      - CREATE_CUSDT_XAU=1, CREATE_CUSDC_XAU=1, CREATE_CEURT_XAU=1
 */
contract CreatePublicXAUPoolsChain138 is Script {
    address internal constant CHAIN138_CUSDT = 0x93E66202A11B1772E55407B32B44e5Cd8eda7f22;
    address internal constant CHAIN138_CUSDC = 0xf22258f57794CC8E06237084b353Ab30fFfa640b;
    address internal constant CHAIN138_CEURT = 0xdf4b71c61E5912712C1Bdd451416B9aC26949d72;
    address internal constant CHAIN138_CXAUC = 0x290E52a8819A4fbD0714E517225429aA2B70EC6b;
    address internal constant CHAIN138_CXAUT = 0x94e408E26c6FD8F4ee00b54dF19082FDA07dC96E;

    uint256 internal constant LP_FEE_BPS = 3;
    uint256 internal constant INITIAL_PRICE_1E18 = 1e18;
    uint256 internal constant K_50PCT = 0.5e18;
    bool internal constant USE_TWAP = true;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address integrationAddr = vm.envOr("DODOPMM_INTEGRATION", address(0));
        if (integrationAddr == address(0)) integrationAddr = vm.envOr("DODOPMM_INTEGRATION_ADDRESS", address(0));
        require(integrationAddr != address(0), "DODOPMM_INTEGRATION not set");

        address xau = vm.envOr("XAU_ADDRESS_138", address(0));
        if (xau == address(0)) xau = vm.envOr("CXAUC_ADDRESS_138", address(0));
        if (xau == address(0)) xau = vm.envOr("CXAUT_ADDRESS_138", address(0));
        if (xau == address(0) && block.chainid == 138) xau = CHAIN138_CXAUC;
        require(xau != address(0), "XAU anchor not set");

        address cUSDT = vm.envOr("COMPLIANT_USDT_ADDRESS", address(0));
        address cUSDC = vm.envOr("COMPLIANT_USDC_ADDRESS", address(0));
        address cEURT = vm.envOr("cEURT_ADDRESS_138", address(0));

        if (cUSDT == address(0) && block.chainid == 138) cUSDT = CHAIN138_CUSDT;
        if (cUSDC == address(0) && block.chainid == 138) cUSDC = CHAIN138_CUSDC;
        if (cEURT == address(0) && block.chainid == 138) cEURT = CHAIN138_CEURT;

        bool createCUSDT = vm.envOr("CREATE_CUSDT_XAU", true);
        bool createCUSDC = vm.envOr("CREATE_CUSDC_XAU", true);
        bool createCEURT = vm.envOr("CREATE_CEURT_XAU", true);

        DODOPMMIntegration integration = DODOPMMIntegration(integrationAddr);

        vm.startBroadcast(pk);

        if (createCUSDT && cUSDT != address(0)) {
            _createIfMissing(integration, cUSDT, xau, "cUSDT/XAU");
        }
        if (createCUSDC && cUSDC != address(0)) {
            _createIfMissing(integration, cUSDC, xau, "cUSDC/XAU");
        }
        if (createCEURT && cEURT != address(0)) {
            _createIfMissing(integration, cEURT, xau, "cEURT/XAU");
        }

        vm.stopBroadcast();

        console.log("Public XAU pools checked against anchor:", xau == CHAIN138_CXAUC ? "cXAUC" : (xau == CHAIN138_CXAUT ? "cXAUT" : "custom"));
        console.log("Default Chain 138 XAU anchors:", vm.toString(CHAIN138_CXAUC), vm.toString(CHAIN138_CXAUT));
    }

    function _createIfMissing(
        DODOPMMIntegration integration,
        address base,
        address quote,
        string memory label
    ) internal {
        address existing = integration.pools(base, quote);
        if (existing != address(0)) {
            console.log(label, "already exists at", existing);
            return;
        }

        address pool = integration.createPool(base, quote, LP_FEE_BPS, INITIAL_PRICE_1E18, K_50PCT, USE_TWAP);
        console.log(label, "created at", pool);
    }
}
