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

import "forge-std/Script.sol";
import {RWATokenRegistry} from "../../../contracts/rwa/RWATokenRegistry.sol";
import {UniversalAssetRegistry} from "../../../contracts/registry/UniversalAssetRegistry.sol";
import {IRWAToken} from "../../../contracts/rwa/IRWAToken.sol";

/**
 * @title RegisterRWAIndicesInUAR138
 * @notice Register deployed Li* tokens in UniversalAssetRegistry (run after factory deploy + REGISTRAR_ROLE grant).
 * @dev Requires RWA_TOKEN_REGISTRY and UNIVERSAL_ASSET_REGISTRY. Broadcaster must hold UAR REGISTRAR_ROLE
 *      or factory must have been granted REGISTRAR_ROLE and this script calls via factory (not implemented — direct UAR calls).
 *
 * Env: PRIVATE_KEY, RWA_TOKEN_REGISTRY, UNIVERSAL_ASSET_REGISTRY
 * Optional: LI_TICKERS=LiXAU,LiPMG,LiBMG1,LiBMG2,LiBMG3 (default all five)
 */
contract RegisterRWAIndicesInUAR138 is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address registryAddr = vm.envAddress("RWA_TOKEN_REGISTRY");
        address uarAddr = vm.envAddress("UNIVERSAL_ASSET_REGISTRY");

        RWATokenRegistry registry = RWATokenRegistry(registryAddr);
        UniversalAssetRegistry uar = UniversalAssetRegistry(uarAddr);

        vm.startBroadcast(pk);

        string memory tickersEnv = vm.envOr("LI_TICKERS", string(""));
        if (bytes(tickersEnv).length > 0) {
            string[] memory parts = vm.split(tickersEnv, ",");
            for (uint256 i = 0; i < parts.length; i++) {
                _registerOne(registry, uar, parts[i]);
            }
        } else {
            _registerOne(registry, uar, "LiXAU");
            _registerOne(registry, uar, "LiPMG");
            _registerOne(registry, uar, "LiBMG1");
            _registerOne(registry, uar, "LiBMG2");
            _registerOne(registry, uar, "LiBMG3");
        }

        vm.stopBroadcast();
    }

    function _registerOne(RWATokenRegistry registry, UniversalAssetRegistry uar, string memory ticker) internal {
        address token = registry.getToken(ticker);
        require(token != address(0), "token missing");
        IRWAToken rwa = IRWAToken(token);
        uar.registerRWAIndexAsset(
            token,
            _nameFor(ticker),
            ticker,
            6,
            "International"
        );
        console.log("UAR registered", ticker, token);
    }

    function _nameFor(string memory ticker) internal pure returns (string memory) {
        if (keccak256(bytes(ticker)) == keccak256("LiXAU")) return "XAU Liquidity Index (M00)";
        if (keccak256(bytes(ticker)) == keccak256("LiPMG")) return "Precious Metals Group Index (M00)";
        if (keccak256(bytes(ticker)) == keccak256("LiBMG1")) return "Base Metals Group Index 1 (M00)";
        if (keccak256(bytes(ticker)) == keccak256("LiBMG2")) return "Base Metals Group Index 2 (M00)";
        if (keccak256(bytes(ticker)) == keccak256("LiBMG3")) return "Base Metals Group Index 3 (M00)";
        return ticker;
    }
}
