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

import {Script, console} from "forge-std/Script.sol";
import {UniversalCCIPFlashBridgeAdapter} from "../../contracts/flash/UniversalCCIPFlashBridgeAdapter.sol";
import {CrossChainFlashRepayReceiver} from "../../contracts/flash/CrossChainFlashRepayReceiver.sol";
import {CrossChainFlashVaultCreditReceiver} from "../../contracts/flash/CrossChainFlashVaultCreditReceiver.sol";

/**
 * @title DeployCrossChainFlashInfrastructure
 * @notice Deploys the Chain 138 cross-chain flash adapter plus both CCIP receivers.
 *
 * Env:
 *   PRIVATE_KEY                     required
 *   FLASH_UNIVERSAL_CCIP_BRIDGE     optional; fallback UNIVERSAL_CCIP_BRIDGE
 *   FLASH_CCIP_ROUTER               optional default router for both receivers
 *   FLASH_REPAY_RECEIVER_ROUTER     optional; fallback FLASH_CCIP_ROUTER / CCIP_ROUTER*
 *   FLASH_VAULT_CREDIT_ROUTER       optional; fallback FLASH_CCIP_ROUTER / CCIP_ROUTER*
 *   CCIP_ROUTER                     optional fallback
 *   CCIP_ROUTER_ADDRESS             optional fallback
 *   CCIP_ROUTER_CHAIN138            optional fallback
 *
 * Usage:
 *   forge script script/deploy/DeployCrossChainFlashInfrastructure.s.sol:DeployCrossChainFlashInfrastructure \
 *     --rpc-url $RPC_URL_138 --broadcast --with-gas-price 1000000000 -vvvv
 */
contract DeployCrossChainFlashInfrastructure is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);

        address universalBridge = vm.envOr("FLASH_UNIVERSAL_CCIP_BRIDGE", address(0));
        if (universalBridge == address(0)) {
            universalBridge = vm.envOr("UNIVERSAL_CCIP_BRIDGE", address(0));
        }
        require(universalBridge != address(0), "FLASH_UNIVERSAL_CCIP_BRIDGE or UNIVERSAL_CCIP_BRIDGE not set");

        address router = vm.envOr("FLASH_CCIP_ROUTER", address(0));
        if (router == address(0)) {
            router = vm.envOr("CCIP_ROUTER", address(0));
        }
        if (router == address(0)) {
            router = vm.envOr("CCIP_ROUTER_ADDRESS", address(0));
        }
        if (router == address(0)) {
            router = vm.envOr("CCIP_ROUTER_CHAIN138", address(0));
        }
        require(router != address(0), "FLASH_CCIP_ROUTER or CCIP_ROUTER* not set");

        address repayReceiverRouter = vm.envOr("FLASH_REPAY_RECEIVER_ROUTER", router);
        address vaultCreditRouter = vm.envOr("FLASH_VAULT_CREDIT_ROUTER", router);

        console.log("Deployer:", deployer);
        console.log("UniversalCCIPBridge:", universalBridge);
        console.log("Repay receiver router:", repayReceiverRouter);
        console.log("Vault credit receiver router:", vaultCreditRouter);

        vm.startBroadcast(pk);

        UniversalCCIPFlashBridgeAdapter adapter = new UniversalCCIPFlashBridgeAdapter(universalBridge);
        CrossChainFlashRepayReceiver repayReceiver = new CrossChainFlashRepayReceiver(repayReceiverRouter);
        CrossChainFlashVaultCreditReceiver vaultCreditReceiver =
            new CrossChainFlashVaultCreditReceiver(vaultCreditRouter);

        vm.stopBroadcast();

        console.log("UniversalCCIPFlashBridgeAdapter:", address(adapter));
        console.log("CrossChainFlashRepayReceiver:", address(repayReceiver));
        console.log("CrossChainFlashVaultCreditReceiver:", address(vaultCreditReceiver));
        console.log("Export: CROSS_CHAIN_FLASH_BRIDGE_ADAPTER=%s", vm.toString(address(adapter)));
        console.log("Export: CROSS_CHAIN_FLASH_REPAY_RECEIVER=%s", vm.toString(address(repayReceiver)));
        console.log(
            "Export: CROSS_CHAIN_FLASH_VAULT_CREDIT_RECEIVER=%s",
            vm.toString(address(vaultCreditReceiver))
        );
    }
}
