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

import {Script, console} from "forge-std/Script.sol";
import {VaultFactory} from "../../../contracts/vault/VaultFactory.sol";

/**
 * @title DeployAcVdcSdcVaults
 * @notice Single script to create all ac* / vdc* / sdc* vaults via VaultFactory.createVaultWithDecimals.
 * @dev Run after DeployVaultSystem. Uses same owner/entity and base token addresses per chain for consistency.
 *      See docs/runbooks/MULTI_CHAIN_EXECUTION_DETERMINISTIC_DEPLOYMENT.md (DepositToken/DebtToken).
 *
 * Env:
 *   PRIVATE_KEY          - Deployer (must have VAULT_DEPLOYER_ROLE on VaultFactory).
 *   VAULT_FACTORY_ADDRESS - VaultFactory contract address.
 *   OWNER                - (optional) Vault owner; default deployer.
 *   ENTITY               - (optional) Regulated entity; default deployer.
 *   CUSDC_ADDRESS_138    - (optional) Compliant USDC; if set, creates vault → acUSDC + vdcUSDC.
 *   CUSDT_ADDRESS_138    - (optional) Compliant USDT; if set, creates vault → acUSDT + vdcUSDT.
 *   COMPLIANT_USDC_ADDRESS, COMPLIANT_USDT_ADDRESS - Fallback env names if CUSDC/CUSDT_138 unset.
 */
contract DeployAcVdcSdcVaults is Script {
    uint8 constant DECIMALS = 6;
    bool constant DEBT_TRANSFERABLE = true;

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

        address factoryAddr = vm.envAddress("VAULT_FACTORY_ADDRESS");
        require(factoryAddr != address(0), "VAULT_FACTORY_ADDRESS required");
        VaultFactory factory = VaultFactory(factoryAddr);

        address cUsdc = _getToken("CUSDC_ADDRESS_138", "COMPLIANT_USDC_ADDRESS");
        address cUsdt = _getToken("CUSDT_ADDRESS_138", "COMPLIANT_USDT_ADDRESS");

        vm.startBroadcast(pk);

        if (cUsdc != address(0)) {
            (address vault, address depositToken, address debtToken) = factory.createVaultWithDecimals(
                owner,
                entity,
                cUsdc,
                cUsdc,
                DECIMALS,
                DECIMALS,
                DEBT_TRANSFERABLE
            );
            console.log("USDC vault:", vault);
            console.log("acUSDC (deposit):", depositToken);
            console.log("vdcUSDC (debt):", debtToken);
        }

        if (cUsdt != address(0)) {
            (address vault, address depositToken, address debtToken) = factory.createVaultWithDecimals(
                owner,
                entity,
                cUsdt,
                cUsdt,
                DECIMALS,
                DECIMALS,
                DEBT_TRANSFERABLE
            );
            console.log("USDT vault:", vault);
            console.log("acUSDT (deposit):", depositToken);
            console.log("vdcUSDT (debt):", debtToken);
        }

        vm.stopBroadcast();
    }

    function _getToken(string memory primary, string memory altKey) internal view returns (address) {
        address a = vm.envOr(primary, address(0));
        if (a != address(0)) return a;
        return vm.envOr(altKey, address(0));
    }
}
