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

import {Script, console} from "forge-std/Script.sol";
import {LiquidityPoolETH} from "../../../contracts/bridge/trustless/LiquidityPoolETH.sol";

/**
 * @title FundMainnetLP
 * @notice Add ETH and/or WETH liquidity to the mainnet LiquidityPoolETH (G4).
 * @dev Env: PRIVATE_KEY, LIQUIDITY_POOL_ETH_MAINNET (or LIQUIDITY_POOL), ETHEREUM_MAINNET_RPC.
 *      Optional: FUND_ETH_AMOUNT_WEI (native ETH), FUND_WETH_AMOUNT_WEI (WETH to deposit).
 *      If both zero/unset, script exits without error (dry run).
 */
contract FundMainnetLP is Script {
    function run() external {
        address poolAddr = vm.envOr("LIQUIDITY_POOL_ETH_MAINNET", vm.envOr("LIQUIDITY_POOL", address(0)));
        if (poolAddr == address(0)) {
            console.log("Set LIQUIDITY_POOL_ETH_MAINNET (or LIQUIDITY_POOL) in .env");
            return;
        }
        uint256 ethAmount = vm.envOr("FUND_ETH_AMOUNT_WEI", uint256(0));
        uint256 wethAmount = vm.envOr("FUND_WETH_AMOUNT_WEI", uint256(0));
        if (ethAmount == 0 && wethAmount == 0) {
            console.log("Set FUND_ETH_AMOUNT_WEI and/or FUND_WETH_AMOUNT_WEI to fund the LP");
            return;
        }

        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        LiquidityPoolETH pool = LiquidityPoolETH(payable(poolAddr));

        vm.startBroadcast(deployerPrivateKey);

        if (ethAmount > 0) {
            pool.provideLiquidity{value: ethAmount}(LiquidityPoolETH.AssetType.ETH);
            console.log("Provided ETH liquidity:", ethAmount);
        }
        if (wethAmount > 0) {
            address weth = pool.weth();
            // Approve and deposit WETH
            (bool ok,) = weth.call(abi.encodeWithSignature("approve(address,uint256)", poolAddr, wethAmount));
            require(ok, "WETH approve failed");
            pool.depositWETH(wethAmount);
            console.log("Provided WETH liquidity:", wethAmount);
        }

        vm.stopBroadcast();
        console.log("LP funding done.");
    }
}
