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

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

interface IDODOPMMIntegrationSeed {
    function isRegisteredPool(address pool) external view returns (bool);
    function addLiquidity(address pool, uint256 baseAmount, uint256 quoteAmount)
        external
        returns (uint256 baseShare, uint256 quoteShare, uint256 lpShare);
}

interface IDODOPMMPoolSeed {
    function _BASE_TOKEN_() external view returns (address);
    function _QUOTE_TOKEN_() external view returns (address);
    function getVaultReserve() external view returns (uint256 baseReserve, uint256 quoteReserve);
}

/// @notice Permanently seed defended mainnet cWUSDC/USDC or cWUSDT/USDT DODO pools via addLiquidity.
/// @dev Uses deployer inventory — not flash (flash must repay same block). Pair with Aave borrow + vault deposit off-chain.
contract SeedMainnetCwStablePoolPermanent is Script {
    using SafeERC20 for IERC20;

    address internal constant DEFAULT_INTEGRATION = 0xa9F284eD010f4F7d7F8F201742b49b9f58e29b84;
    address internal constant POOL_CWUSDC_USDC = 0x69776fc607e9edA8042e320e7e43f54d06c68f0E;
    /// @dev Registered on DODOPMMIntegration.pools(cWUSDT, USDT); 0x99d012… is an unregistered duplicate.
    address internal constant POOL_CWUSDT_USDT = 0x79156F6B7bf71a1B72D78189B540A89A6C13F6FC;
    address internal constant CWUSDC = 0x2de5F116bFcE3d0f922d9C8351e0c5Fc24b9284a;
    address internal constant CWUSDT = 0xaF5017d0163ecb99D9B5D94e3b4D7b09Af44D8AE;
    address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        string memory rail = vm.envOr("CW_STABLE_RAIL", string("USDC"));
        uint256 baseAmount = vm.envUint("SEED_BASE_AMOUNT_RAW");
        uint256 quoteAmount = vm.envUint("SEED_QUOTE_AMOUNT_RAW");
        address integration = vm.envOr("DODO_PMM_INTEGRATION_MAINNET", DEFAULT_INTEGRATION);

        (address pool, address baseToken, address quoteToken) = _resolveRail(rail);

        IDODOPMMPoolSeed poolView = IDODOPMMPoolSeed(pool);
        require(poolView._BASE_TOKEN_() == baseToken, "base mismatch");
        require(poolView._QUOTE_TOKEN_() == quoteToken, "quote mismatch");
        require(IDODOPMMIntegrationSeed(integration).isRegisteredPool(pool), "pool not registered on integration");

        (uint256 baseBefore, uint256 quoteBefore) = poolView.getVaultReserve();
        console.log("deployer", deployer);
        console.log("rail", rail);
        console.log("pool", pool);
        console.log("baseAmount", baseAmount);
        console.log("quoteAmount", quoteAmount);
        console.log("baseReserveBefore", baseBefore);
        console.log("quoteReserveBefore", quoteBefore);

        vm.startBroadcast(pk);
        IERC20(baseToken).forceApprove(integration, baseAmount);
        IERC20(quoteToken).forceApprove(integration, quoteAmount);
        (uint256 baseShare, uint256 quoteShare, uint256 lpShare) =
            IDODOPMMIntegrationSeed(integration).addLiquidity(pool, baseAmount, quoteAmount);
        IERC20(baseToken).forceApprove(integration, 0);
        IERC20(quoteToken).forceApprove(integration, 0);
        vm.stopBroadcast();

        (uint256 baseAfter, uint256 quoteAfter) = poolView.getVaultReserve();
        console.log("baseShare", baseShare);
        console.log("quoteShare", quoteShare);
        console.log("lpShare", lpShare);
        console.log("baseReserveAfter", baseAfter);
        console.log("quoteReserveAfter", quoteAfter);
    }

    function _resolveRail(string memory rail)
        internal
        pure
        returns (address pool, address baseToken, address quoteToken)
    {
        bytes32 key = keccak256(bytes(rail));
        if (key == keccak256(bytes("USDC"))) {
            return (POOL_CWUSDC_USDC, CWUSDC, USDC);
        }
        if (key == keccak256(bytes("USDT"))) {
            return (POOL_CWUSDT_USDT, CWUSDT, USDT);
        }
        revert("CW_STABLE_RAIL must be USDC or USDT");
    }
}
