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

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

/**
 * @title DeployGasCanonicalTokens
 * @notice Deploy Wave 1 gas-native canonical c* tokens on Chain 138.
 *
 * Env:
 *   PRIVATE_KEY (required)
 *   GAS_FAMILY (optional) - deploy one family only: eth_mainnet, eth_l2, bnb, pol, avax, cro, xdai, celo, wemix
 *   GAS_INITIAL_OWNER (optional, defaults to deployer)
 *   GAS_ADMIN (optional, defaults to deployer)
 *   GAS_INITIAL_SUPPLY (optional, defaults to 0)
 *   DEPLOY_GAS_<FAMILY>=0 to skip a family when GAS_FAMILY is unset
 */
contract DeployGasCanonicalTokens is Script {
    uint8 internal constant DECIMALS = 18;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address owner = vm.envOr("GAS_INITIAL_OWNER", deployer);
        address admin = vm.envOr("GAS_ADMIN", deployer);
        uint256 initialSupply = vm.envOr("GAS_INITIAL_SUPPLY", uint256(0));
        string memory targetFamily = vm.envOr("GAS_FAMILY", string(""));

        vm.startBroadcast(pk);

        _deployOne(owner, admin, initialSupply, targetFamily, "eth_mainnet", "DEPLOY_GAS_ETH_MAINNET", "Ethereum Mainnet Gas (Compliant)", "cETH", "ETH");
        _deployOne(owner, admin, initialSupply, targetFamily, "eth_l2", "DEPLOY_GAS_ETH_L2", "Ethereum L2 Gas (Compliant)", "cETHL2", "ETH");
        _deployOne(owner, admin, initialSupply, targetFamily, "bnb", "DEPLOY_GAS_BNB", "BNB Gas (Compliant)", "cBNB", "BNB");
        _deployOne(owner, admin, initialSupply, targetFamily, "pol", "DEPLOY_GAS_POL", "Polygon Gas (Compliant)", "cPOL", "POL");
        _deployOne(owner, admin, initialSupply, targetFamily, "avax", "DEPLOY_GAS_AVAX", "Avalanche Gas (Compliant)", "cAVAX", "AVAX");
        _deployOne(owner, admin, initialSupply, targetFamily, "cro", "DEPLOY_GAS_CRO", "Cronos Gas (Compliant)", "cCRO", "CRO");
        _deployOne(owner, admin, initialSupply, targetFamily, "xdai", "DEPLOY_GAS_XDAI", "Gnosis Gas (Compliant)", "cXDAI", "XDAI");
        _deployOne(owner, admin, initialSupply, targetFamily, "celo", "DEPLOY_GAS_CELO", "Celo Gas (Compliant)", "cCELO", "CELO");
        _deployOne(owner, admin, initialSupply, targetFamily, "wemix", "DEPLOY_GAS_WEMIX", "Wemix Gas (Compliant)", "cWEMIX", "WEMIX");

        vm.stopBroadcast();
    }

    function _deployOne(
        address owner,
        address admin,
        uint256 initialSupply,
        string memory targetFamily,
        string memory familyKey,
        string memory envFlag,
        string memory name,
        string memory symbol,
        string memory currencyCode
    ) internal {
        if (!_shouldDeploy(targetFamily, familyKey, envFlag)) return;

        CompliantFiatToken token = new CompliantFiatToken(
            name,
            symbol,
            DECIMALS,
            currencyCode,
            owner,
            admin,
            initialSupply
        );

        console.log(symbol, address(token));
        console.log("  familyKey", familyKey);
        console.log("  owner", owner);
        console.log("  admin", admin);
        console.log("  initialSupply", initialSupply);
    }

    function _shouldDeploy(
        string memory targetFamily,
        string memory familyKey,
        string memory envFlag
    ) internal view returns (bool) {
        if (bytes(targetFamily).length != 0) {
            return keccak256(bytes(targetFamily)) == keccak256(bytes(familyKey));
        }
        return vm.envOr(envFlag, uint256(1)) != 0;
    }
}
