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

import {Script, console} from "forge-std/Script.sol";
import {CompliantUSDTTokenV2} from "../../contracts/tokens/CompliantUSDTTokenV2.sol";
import {CompliantUSDCTokenV2} from "../../contracts/tokens/CompliantUSDCTokenV2.sol";

/**
 * @title DeployCompliantFiatTokensV2ForChain
 * @notice Deploy canonical cUSDT V2 / cUSDC V2 contracts to the current chain.
 * @dev Defaults to safe pre-cutover posture: new addresses with forwardCanonical disabled unless env overrides it.
 *
 * Env:
 *   PRIVATE_KEY (required)
 *   INITIAL_OPERATOR (optional; default deployer)
 *   ADMIN (optional; default deployer)
 *   OWNER (optional alias for ADMIN when ADMIN unset)
 *   INITIAL_SUPPLY (optional; default 1_000_000e6)
 *   FORWARD_CANONICAL=1 to mark deployed V2 as forward canonical immediately
 *   DEPLOY_CUSDT_V2=1 / DEPLOY_CUSDC_V2=1 (default both 1)
 */
contract DeployCompliantFiatTokensV2ForChain is Script {
    uint256 internal constant DEFAULT_INITIAL_SUPPLY = 1_000_000 * 10 ** 6;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address initialOperator = vm.envOr("INITIAL_OPERATOR", deployer);
        address ownerAlias = vm.envOr("OWNER", deployer);
        address admin = vm.envOr("ADMIN", ownerAlias);
        uint256 initialSupply = vm.envOr("INITIAL_SUPPLY", DEFAULT_INITIAL_SUPPLY);
        bool forwardCanonical = vm.envOr("FORWARD_CANONICAL", false);

        vm.startBroadcast(pk);

        if (vm.envOr("DEPLOY_CUSDT_V2", uint256(1)) != 0) {
            CompliantUSDTTokenV2 cusdtV2 =
                new CompliantUSDTTokenV2(initialOperator, admin, initialSupply, forwardCanonical);
            console.log("cUSDT_V2", address(cusdtV2));
            console.log("cUSDT_V2_admin", admin);
            console.log("cUSDT_V2_initialOperator", initialOperator);
            console.log("cUSDT_V2_forwardCanonical", forwardCanonical);
        }

        if (vm.envOr("DEPLOY_CUSDC_V2", uint256(1)) != 0) {
            CompliantUSDCTokenV2 cusdcV2 =
                new CompliantUSDCTokenV2(initialOperator, admin, initialSupply, forwardCanonical);
            console.log("cUSDC_V2", address(cusdcV2));
            console.log("cUSDC_V2_admin", admin);
            console.log("cUSDC_V2_initialOperator", initialOperator);
            console.log("cUSDC_V2_forwardCanonical", forwardCanonical);
        }

        vm.stopBroadcast();
    }
}
