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

import {Script, console} from "forge-std/Script.sol";
import {DODOPMMIntegration} from "../../contracts/dex/DODOPMMIntegration.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
 * @title AddLiquidityCUSDWCUSDCV2PoolChain138
 * @notice Add liquidity to the Chain 138 cUSDW / cUSDC_V2 DODO PMM pool.
 * @dev Env: PRIVATE_KEY, DODO_PMM_INTEGRATION or DODO_PMM_INTEGRATION_ADDRESS,
 *      CUSDW_ADDRESS_138, CUSDC_V2_ADDRESS_138, POOL_CUSDWCUSDCV2,
 *      ADD_LIQUIDITY_CUSDWCUSDCV2_BASE, ADD_LIQUIDITY_CUSDWCUSDCV2_QUOTE.
 *      Optional fallback amounts:
 *      - ADD_LIQUIDITY_BASE_AMOUNT
 *      - ADD_LIQUIDITY_QUOTE_AMOUNT
 *      Optional: NEXT_NONCE.
 */
contract AddLiquidityCUSDWCUSDCV2PoolChain138 is Script {
    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");
        address pool = vm.envAddress("POOL_CUSDWCUSDCV2");
        require(cusdw != address(0), "CUSDW_ADDRESS_138 not set");
        require(cusdcV2 != address(0), "CUSDC_V2_ADDRESS_138 not set");
        require(pool != address(0), "POOL_CUSDWCUSDCV2 not set");

        uint256 defaultBase = vm.envOr("ADD_LIQUIDITY_BASE_AMOUNT", uint256(0));
        uint256 defaultQuote = vm.envOr("ADD_LIQUIDITY_QUOTE_AMOUNT", uint256(0));
        uint256 baseAmount = vm.envOr("ADD_LIQUIDITY_CUSDWCUSDCV2_BASE", defaultBase);
        uint256 quoteAmount = vm.envOr("ADD_LIQUIDITY_CUSDWCUSDCV2_QUOTE", defaultQuote);
        require(baseAmount > 0, "ADD_LIQUIDITY_CUSDWCUSDCV2_BASE not set");
        require(quoteAmount > 0, "ADD_LIQUIDITY_CUSDWCUSDCV2_QUOTE not set");

        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);
        IERC20(cusdw).approve(address(integration), type(uint256).max);
        IERC20(cusdcV2).approve(address(integration), type(uint256).max);
        integration.addLiquidity(pool, baseAmount, quoteAmount);
        console.log("Added liquidity to cUSDW/cUSDC_V2 pool:", pool);
        vm.stopBroadcast();
    }
}
