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

import "forge-std/Script.sol";
import {CompliantWrappedToken} from "../../contracts/tokens/CompliantWrappedToken.sol";

/**
 * @title DeployCWTokens
 * @notice Deploy all cW* (CompliantWrappedToken) on the current chain; grant MINTER_ROLE and BURNER_ROLE to bridge.
 * @dev Run with --rpc-url <CHAIN_RPC> --chain-id <ID>. Set CW_BRIDGE_ADDRESS (or CCIP receiver) to receive MINTER_ROLE/BURNER_ROLE.
 *
 * Env:
 *   PRIVATE_KEY (required)
 *   CW_BRIDGE_ADDRESS (required) — address that can mint/burn (e.g. CCIP receiver or custom bridge)
 *   DEPLOY_CWUSDT=1, DEPLOY_CWUSDC=1, DEPLOY_CWEURC=1, ... (default all 1; set 0 to skip a token)
 */
contract DeployCWTokens is Script {
    uint8 constant DECIMALS = 6;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address bridge = vm.envAddress("CW_BRIDGE_ADDRESS");
        require(bridge != address(0), "CW_BRIDGE_ADDRESS required");

        vm.startBroadcast(pk);

        _deployOne(deployer, "Wrapped cUSDT", "cWUSDT", "DEPLOY_CWUSDT", bridge);
        _deployOne(deployer, "Wrapped cUSDC", "cWUSDC", "DEPLOY_CWUSDC", bridge);
        _deployOne(deployer, "Wrapped cEURC", "cWEURC", "DEPLOY_CWEURC", bridge);
        _deployOne(deployer, "Wrapped cEURT", "cWEURT", "DEPLOY_CWEURT", bridge);
        _deployOne(deployer, "Wrapped cGBPC", "cWGBPC", "DEPLOY_CWGBPC", bridge);
        _deployOne(deployer, "Wrapped cGBPT", "cWGBPT", "DEPLOY_CWGBPT", bridge);
        _deployOne(deployer, "Wrapped cAUDC", "cWAUDC", "DEPLOY_CWAUDC", bridge);
        _deployOne(deployer, "Wrapped cJPYC", "cWJPYC", "DEPLOY_CWJPYC", bridge);
        _deployOne(deployer, "Wrapped cCHFC", "cWCHFC", "DEPLOY_CWCHFC", bridge);
        _deployOne(deployer, "Wrapped cCADC", "cWCADC", "DEPLOY_CWCADC", bridge);
        _deployOne(deployer, "Wrapped cXAUC", "cWXAUC", "DEPLOY_CWXAUC", bridge);
        _deployOne(deployer, "Wrapped cXAUT", "cWXAUT", "DEPLOY_CWXAUT", bridge);

        vm.stopBroadcast();
    }

    function _deployOne(
        address admin,
        string memory name,
        string memory symbol,
        string memory envKey,
        address bridge
    ) internal {
        if (vm.envOr(envKey, uint256(1)) == 0) return;
        CompliantWrappedToken t = new CompliantWrappedToken(name, symbol, DECIMALS, admin);
        t.grantRole(t.MINTER_ROLE(), bridge);
        t.grantRole(t.BURNER_ROLE(), bridge);
        console.log(symbol, address(t));
    }
}
