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

import "forge-std/Script.sol";
import "../../contracts/bridge/integration/VaultBridgeIntegration.sol";
import "../../contracts/bridge/integration/WTokenBridgeIntegration.sol";
import "../../contracts/bridge/integration/eMoneyBridgeIntegration.sol";
import "../../contracts/bridge/integration/WTokenReserveVerifier.sol";
import "../../contracts/bridge/integration/WTokenComplianceEnforcer.sol";
import "../../contracts/bridge/integration/eMoneyPolicyEnforcer.sol";

/**
 * @title DeployBridgeIntegrations
 * @notice Deployment script for all bridge integrations
 */
contract DeployBridgeIntegrations is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address admin = vm.addr(deployerPrivateKey);

        // Get existing contract addresses from environment
        address bridgeRegistry = vm.envAddress("BRIDGE_REGISTRY");
        address bridgeEscrowVault = vm.envAddress("BRIDGE_ESCROW_VAULT");
        address vaultFactory = vm.envAddress("VAULT_FACTORY");
        address tokenFactory = vm.envAddress("TOKEN_FACTORY");
        address wTokenRegistry = vm.envAddress("W_TOKEN_REGISTRY");
        address reserveOracle = vm.envAddress("RESERVE_ORACLE");
        address complianceGuard = vm.envAddress("COMPLIANCE_GUARD");
        address policyManager = vm.envAddress("POLICY_MANAGER");
        address eMoneyComplianceRegistry = vm.envAddress("EMONEY_COMPLIANCE_REGISTRY");

        vm.startBroadcast(deployerPrivateKey);

        console.log("Deploying Bridge Integrations...");
        console.log("Admin:", admin);

        // 1. Deploy Vault Bridge Integration
        console.log("\n1. Deploying VaultBridgeIntegration...");
        VaultBridgeIntegration vaultBridgeIntegration = new VaultBridgeIntegration(
            admin,
            vaultFactory,
            bridgeRegistry
        );
        console.log("VaultBridgeIntegration:", address(vaultBridgeIntegration));

        // Grant registrar role to integration
        // vm.prank(bridgeRegistry);
        // BridgeRegistry(bridgeRegistry).grantRole(keccak256("REGISTRAR_ROLE"), address(vaultBridgeIntegration));

        // 2. Deploy W Token Bridge Integration
        console.log("\n2. Deploying WTokenBridgeIntegration...");
        WTokenBridgeIntegration wTokenBridgeIntegration = new WTokenBridgeIntegration(
            admin,
            tokenFactory,
            bridgeRegistry,
            wTokenRegistry
        );
        console.log("WTokenBridgeIntegration:", address(wTokenBridgeIntegration));

        // 3. Deploy eMoney Bridge Integration
        console.log("\n3. Deploying eMoneyBridgeIntegration...");
        eMoneyBridgeIntegration eMoneyBridgeIntegrationContract = new eMoneyBridgeIntegration(
            admin,
            bridgeRegistry
        );
        console.log("eMoneyBridgeIntegration:", address(eMoneyBridgeIntegrationContract));

        // 4. Deploy W Token Reserve Verifier
        console.log("\n4. Deploying WTokenReserveVerifier...");
        WTokenReserveVerifier wTokenReserveVerifier = new WTokenReserveVerifier(
            admin,
            bridgeEscrowVault,
            reserveOracle
        );
        console.log("WTokenReserveVerifier:", address(wTokenReserveVerifier));

        // 5. Deploy W Token Compliance Enforcer
        console.log("\n5. Deploying WTokenComplianceEnforcer...");
        WTokenComplianceEnforcer wTokenComplianceEnforcer = new WTokenComplianceEnforcer(
            admin,
            bridgeEscrowVault,
            complianceGuard
        );
        console.log("WTokenComplianceEnforcer:", address(wTokenComplianceEnforcer));

        // 6. Deploy eMoney Policy Enforcer
        console.log("\n6. Deploying eMoneyPolicyEnforcer...");
        eMoneyPolicyEnforcer eMoneyPolicyEnforcerContract = new eMoneyPolicyEnforcer(
            admin,
            bridgeEscrowVault,
            policyManager,
            eMoneyComplianceRegistry
        );
        console.log("eMoneyPolicyEnforcer:", address(eMoneyPolicyEnforcerContract));

        vm.stopBroadcast();

        console.log("\n=== Deployment Complete ===");
        console.log("VaultBridgeIntegration:", address(vaultBridgeIntegration));
        console.log("WTokenBridgeIntegration:", address(wTokenBridgeIntegration));
        console.log("eMoneyBridgeIntegration:", address(eMoneyBridgeIntegrationContract));
        console.log("WTokenReserveVerifier:", address(wTokenReserveVerifier));
        console.log("WTokenComplianceEnforcer:", address(wTokenComplianceEnforcer));
        console.log("eMoneyPolicyEnforcer:", address(eMoneyPolicyEnforcerContract));
        console.log("\nNext Steps:");
        console.log("1. Grant REGISTRAR_ROLE to integration contracts in BridgeRegistry");
        console.log("2. Register tokens with bridge integrations");
        console.log("3. Configure reserve verifier for W tokens");
        console.log("4. Configure compliance enforcer for W tokens");
        console.log("5. Configure policy enforcer for eMoney tokens");
    }
}
