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

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

interface IMintableM0 {
    function mint(address to, uint256 amount) external;
    function mint(address to, uint256 amount, bytes32 reasonHash) external;
}

/// @notice Mint M0 (cUSDT) so that S1 <= 5 * S0 for the health line (policy cap remediation).
contract RemediateOmnlM1Cap is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address tokenM0 = vm.envAddress("OMNL_TOKEN_M0");
        address tokenM1 = vm.envAddress("OMNL_TOKEN_M1");
        address mintTo = vm.envOr("OMNL_M1_REMEDIATION_MINT_TO", vm.addr(pk));

        uint256 s0 = IERC20(tokenM0).totalSupply();
        uint256 s1 = IERC20(tokenM1).totalSupply();

        uint256 requiredS0 = (s1 + 4) / 5;
        if (s0 >= requiredS0) {
            console2.log("No mint required; s0", s0, "requiredS0", requiredS0);
            return;
        }
        uint256 delta = requiredS0 - s0;
        bytes32 reason = keccak256("OMNL-M1-CAP-REMEDIATION");

        vm.startBroadcast(pk);
        IMintableM0 m0 = IMintableM0(tokenM0);
        try m0.mint(mintTo, delta, reason) {
        } catch {
            m0.mint(mintTo, delta);
        }
        vm.stopBroadcast();

        uint256 s0After = IERC20(tokenM0).totalSupply();
        uint256 s1After = IERC20(tokenM1).totalSupply();
        bool m1OkAfter = s1After <= s0After * 5;
        console2.log("Minted delta", delta, "to", mintTo);
        console2.log("s0After", s0After);
        console2.log("s1After", s1After);
        console2.log("m1OkAfter", m1OkAfter);
    }
}
