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

import "forge-std/Script.sol";
import "../../contracts/dbis/DBIS_ParticipantRegistry.sol";
import "../../contracts/dbis/DBIS_GRU_MintController.sol";
import "../../contracts/dbis/StablecoinReferenceRegistry.sol";

/**
 * @title WireDBISRailPostDeploy
 * @notice Post-deploy wiring: stablecoin registry, optional GRU token, seed participants.
 * @dev Env: PRIVATE_KEY; contract addresses from deploy report or env DBIS_* vars.
 */
contract WireDBISRailPostDeploy is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address admin = vm.envOr("ADMIN_ADDRESS", vm.addr(pk));

        address stableRegAddr = vm.envAddress("DBIS_STABLECOIN_REGISTRY");
        address mintCtrlAddr = vm.envAddress("DBIS_GRU_MINT_CONTROLLER");
        address participantRegAddr = vm.envAddress("DBIS_PARTICIPANT_REGISTRY");

        StablecoinReferenceRegistry stableReg = StablecoinReferenceRegistry(stableRegAddr);
        DBIS_GRU_MintController mintController = DBIS_GRU_MintController(mintCtrlAddr);
        DBIS_ParticipantRegistry participantReg = DBIS_ParticipantRegistry(participantRegAddr);

        vm.startBroadcast(pk);

        _registerStablecoin(
            stableReg,
            vm.envAddress("CUSDT_ADDRESS"),
            "cUSDT",
            admin
        );
        _registerStablecoin(
            stableReg,
            vm.envAddress("CUSDC_ADDRESS"),
            "cUSDC",
            admin
        );

        address gruToken = vm.envOr("DBIS_GRU_MINTABLE_TOKEN", address(0));
        if (gruToken != address(0)) {
            mintController.setGruToken(gruToken);
        }

        bytes32 participantId = keccak256("OMNL-OPERATOR");
        DBIS_ParticipantRegistry.Participant memory existing = participantReg.getParticipant(participantId);
        if (existing.participantId == bytes32(0)) {
            address[] memory wallets = new address[](1);
            wallets[0] = admin;
            bytes32[] memory tags = new bytes32[](0);
            participantReg.registerParticipant(
                DBIS_ParticipantRegistry.Participant({
                    participantId: participantId,
                    legalName: "OMNL Fineract Operator",
                    jurisdiction: "MULTI",
                    entityType: DBIS_ParticipantRegistry.EntityType.OPERATOR,
                    status: DBIS_ParticipantRegistry.Status.ACTIVE,
                    policyTags: tags,
                    operationalWallets: wallets
                })
            );
        }

        vm.stopBroadcast();
    }

    function _registerStablecoin(
        StablecoinReferenceRegistry reg,
        address token,
        string memory symbol,
        address pauseAuthority
    ) private {
        if (token == address(0)) return;
        StablecoinReferenceRegistry.StablecoinEntry memory existing = reg.getEntry(token);
        if (existing.exists) return;
        reg.register(
            token,
            symbol,
            "DBIS",
            "compliant_stable",
            "omnl_redeem",
            "EXPLORER_TOKEN_LIST_CROSSCHECK",
            1,
            pauseAuthority
        );
    }
}
