// 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 CreateCUSDWCUSDCV2Pool
 * @notice Create a Chain 138 DODO PMM pool for cUSDW / cUSDC_V2 using the generic createPool path.
 * @dev Assumes repo-native D-WIN-aligned cUSDW on Chain 138 and staged cUSDC V2 as the quote leg.
 *      Env: PRIVATE_KEY, DODO_PMM_INTEGRATION or DODO_PMM_INTEGRATION_ADDRESS,
 *      CUSDW_ADDRESS_138, CUSDC_V2_ADDRESS_138.
 *      Optional params:
 *      - DODO_LP_FEE_BPS (default 3)
 *      - DODO_INITIAL_PRICE_1E18 (default 1e18)
 *      - DODO_K_FACTOR_1E18 (default 0.5e18)
 *      - DODO_ENABLE_TWAP (default true)
 *      - NEXT_NONCE
 */
contract CreateCUSDWCUSDCV2Pool is Script {
    uint256 constant DEFAULT_LP_FEE_BPS = 3;
    uint256 constant DEFAULT_INITIAL_PRICE_1E18 = 1e18;
    uint256 constant DEFAULT_K_50PCT = 0.5e18;
    bool constant DEFAULT_USE_TWAP = true;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address integrationAddr = vm.envAddress("DODO_PMM_INTEGRATION");
        if (integrationAddr == address(0)) {
            integrationAddr = vm.envAddress("DODO_PMM_INTEGRATION_ADDRESS");
        }
        require(integrationAddr != address(0), "DODO_PMM_INTEGRATION not set");

        address cusdw = vm.envAddress("CUSDW_ADDRESS_138");
        address cusdcV2 = vm.envAddress("CUSDC_V2_ADDRESS_138");
        require(cusdw != address(0), "CUSDW_ADDRESS_138 not set");
        require(cusdcV2 != address(0), "CUSDC_V2_ADDRESS_138 not set");

        uint256 lpFeeBps = vm.envOr("DODO_LP_FEE_BPS", DEFAULT_LP_FEE_BPS);
        uint256 initialPrice = vm.envOr("DODO_INITIAL_PRICE_1E18", DEFAULT_INITIAL_PRICE_1E18);
        uint256 kFactor = vm.envOr("DODO_K_FACTOR_1E18", DEFAULT_K_50PCT);
        bool useTwap = vm.envOr("DODO_ENABLE_TWAP", DEFAULT_USE_TWAP);

        address deployer = vm.addr(pk);
        uint64 nextNonce = uint64(vm.envOr("NEXT_NONCE", uint256(0)));
        if (nextNonce > 0) {
            vm.setNonce(deployer, nextNonce);
        }

        DODOPMMIntegration integration = DODOPMMIntegration(integrationAddr);
        vm.startBroadcast(pk);
        address pool = integration.createPool(
            cusdw,
            cusdcV2,
            lpFeeBps,
            initialPrice,
            kFactor,
            useTwap
        );
        console.log("cUSDW/cUSDC_V2 pool created at:", pool);
        vm.stopBroadcast();
    }
}
