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

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

/**
 * @title DeployCompliantFiatTokensForChain
 * @notice Deploy compliant tokens (cUSDT, cUSDC, and optionally cEURC, etc.) to the current chain.
 * @dev Chain-agnostic: run with any RPC (--rpc-url <URL> --chain-id <ID>). Use when CUSDT_<CHAIN> / CUSDC_<CHAIN> are not set.
 *      Owner/admin get mint; deployer is default owner. No CREATE2 required.
 *
 * Env:
 *   PRIVATE_KEY (required)
 *   OWNER, ADMIN (optional; default deployer)
 *   DEPLOY_CUSDT=1, DEPLOY_CUSDC=1 (default both 1)
 *   DEPLOY_CEURC=1, DEPLOY_CEURT=1, ... (optional; deploy extra CompliantFiatToken)
 *
 * XAU: cXAUC/cXAUT — 1 full token = 1 troy ounce Au (see CompliantFiatToken).
 */
contract DeployCompliantFiatTokensForChain is Script {
    uint256 constant DECIMALS = 6;
    uint256 constant INITIAL_SUPPLY = 1_000_000 * 10**6; // 1M units (fiat: currency; XAU: troy oz)

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address owner = vm.envOr("OWNER", deployer);
        address admin = vm.envOr("ADMIN", deployer);

        vm.startBroadcast(pk);

        if (vm.envOr("DEPLOY_CUSDT", uint256(1)) != 0) {
            CompliantUSDT cusdt = new CompliantUSDT(owner, admin);
            console.log("cUSDT", address(cusdt));
        }
        if (vm.envOr("DEPLOY_CUSDC", uint256(1)) != 0) {
            CompliantUSDC cusdc = new CompliantUSDC(owner, admin);
            console.log("cUSDC", address(cusdc));
        }

        if (vm.envOr("DEPLOY_CEURC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cEURC", "Euro Coin (Compliant)", "EUR");
        }
        if (vm.envOr("DEPLOY_CEURT", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cEURT", "Tether EUR (Compliant)", "EUR");
        }
        if (vm.envOr("DEPLOY_CGBPC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cGBPC", "Pound Sterling (Compliant)", "GBP");
        }
        if (vm.envOr("DEPLOY_CGBPT", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cGBPT", "Tether GBP (Compliant)", "GBP");
        }
        if (vm.envOr("DEPLOY_CAUDC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cAUDC", "Australian Dollar (Compliant)", "AUD");
        }
        if (vm.envOr("DEPLOY_CJPYC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cJPYC", "Japanese Yen (Compliant)", "JPY");
        }
        if (vm.envOr("DEPLOY_CCHFC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cCHFC", "Swiss Franc (Compliant)", "CHF");
        }
        if (vm.envOr("DEPLOY_CCADC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cCADC", "Canadian Dollar (Compliant)", "CAD");
        }
        if (vm.envOr("DEPLOY_CXAUC", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cXAUC", "Gold (Compliant)", "XAU");
        }
        if (vm.envOr("DEPLOY_CXAUT", uint256(0)) != 0) {
            _deployFiat(owner, admin, "cXAUT", "Tether XAU (Compliant)", "XAU");
        }

        vm.stopBroadcast();
    }

    function _deployFiat(
        address owner,
        address admin,
        string memory symbol,
        string memory name,
        string memory currencyCode
    ) internal {
        CompliantFiatToken t = new CompliantFiatToken(
            name,
            symbol,
            // forge-lint: disable-next-line(unsafe-typecast) -- DECIMALS is 6 or 18
            uint8(DECIMALS),
            currencyCode,
            owner,
            admin,
            INITIAL_SUPPLY
        );
        console.log(symbol, address(t));
    }
}
