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

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

/**
 * @title DeployPrivatePoolRegistryAndPools
 * @notice Deploy PrivatePoolRegistry and optionally create XAU-anchored private pools (Master Plan Phase 2).
 * @dev Env: PRIVATE_KEY, (optional) PRIVATE_POOL_REGISTRY_ADMIN, DODOPMM_INTEGRATION_ADDRESS,
 *      XAU_ADDRESS_138, COMPLIANT_USDT_ADDRESS, COMPLIANT_USDC_ADDRESS, cEURT_ADDRESS_138.
 *      Deployer must have POOL_MANAGER_ROLE on DODOPMMIntegration to create pools.
 */
contract DeployPrivatePoolRegistryAndPools is Script {
    address internal constant CHAIN138_CXAUC = 0x290E52a8819A4fbD0714E517225429aA2B70EC6b;
    address internal constant CHAIN138_CXAUT = 0x94e408E26c6FD8F4ee00b54dF19082FDA07dC96E;
    uint256 constant LP_FEE_BPS = 3;
    uint256 constant INITIAL_PRICE_1E18 = 1e18;
    uint256 constant K_50PCT = 0.5e18;
    bool constant USE_TWAP = true;

    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        address admin = vm.envOr("PRIVATE_POOL_REGISTRY_ADMIN", deployer);

        vm.startBroadcast(deployerPrivateKey);

        PrivatePoolRegistry registry = new PrivatePoolRegistry(admin);
        console.log("PrivatePoolRegistry deployed at:", vm.toString(address(registry)));

        address integrationAddr = vm.envOr("DODOPMM_INTEGRATION_ADDRESS", address(0));
        if (integrationAddr == address(0)) integrationAddr = vm.envOr("DODO_PMM_INTEGRATION_ADDRESS", address(0));
        if (integrationAddr == address(0)) integrationAddr = vm.envOr("DODOPMM_INTEGRATION", address(0));
        if (integrationAddr == address(0)) integrationAddr = vm.envOr("DODO_PMM_INTEGRATION", address(0));
        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;

        if (integrationAddr != address(0) && xau != address(0)) {
            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 = 0x93E66202A11B1772E55407B32B44e5Cd8eda7f22;
            if (cUSDC == address(0) && block.chainid == 138) cUSDC = 0xf22258f57794CC8E06237084b353Ab30fFfa640b;
            if (cEURT == address(0) && block.chainid == 138) cEURT = 0xdf4b71c61E5912712C1Bdd451416B9aC26949d72;

            DODOPMMIntegration integration = DODOPMMIntegration(integrationAddr);

            if (cUSDT != address(0)) {
                _ensureRegistered(registry, integration, cUSDT, xau, "cUSDT/XAU");
            }
            if (cUSDC != address(0)) {
                _ensureRegistered(registry, integration, cUSDC, xau, "cUSDC/XAU");
            }
            if (cEURT != address(0)) {
                _ensureRegistered(registry, integration, cEURT, xau, "cEURT/XAU");
            }
        } else {
            console.log(
                "Skipping pool creation (set DODOPMM_INTEGRATION_ADDRESS and XAU_ADDRESS_138/CXAUC_ADDRESS_138/CXAUT_ADDRESS_138 to create XAU-anchored pools)"
            );
        }

        vm.stopBroadcast();

        console.log("\n=== Deployment Summary ===");
        console.log("PrivatePoolRegistry:", vm.toString(address(registry)));
        console.log("Admin:", vm.toString(admin));
        console.log("Default XAU anchor on Chain 138:", vm.toString(CHAIN138_CXAUC));
        console.log("Alternate XAU anchor on Chain 138:", vm.toString(CHAIN138_CXAUT));
    }

    function _ensureRegistered(
        PrivatePoolRegistry registry,
        DODOPMMIntegration integration,
        address base,
        address quote,
        string memory label
    ) internal {
        address pool = integration.pools(base, quote);
        if (pool == address(0)) {
            pool = integration.createPool(base, quote, LP_FEE_BPS, INITIAL_PRICE_1E18, K_50PCT, USE_TWAP);
            console.log("Created", label, "pool:", vm.toString(pool));
        } else {
            console.log(label, "pool already exists:", vm.toString(pool));
        }

        if (registry.getPrivatePool(base, quote) == address(0)) {
            registry.register(base, quote, pool);
            console.log("Registered", label, "pool:", vm.toString(pool));
        } else {
            console.log(label, "already registered:", vm.toString(registry.getPrivatePool(base, quote)));
        }
    }
}
