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

import "forge-std/Script.sol";
import {RWATokenRegistry} from "../../../contracts/rwa/RWATokenRegistry.sol";
import {RWATokenFactory} from "../../../contracts/rwa/RWATokenFactory.sol";
import {IRWATokenFactory} from "../../../contracts/rwa/IRWATokenFactory.sol";
import {RWAToken} from "../../../contracts/rwa/RWAToken.sol";

/**
 * @title RedeployLiXauPublisher138
 * @notice Operator recovery when Gnosis Safe co-signer keys are unavailable:
 *         deactivate LiXAU in registry, redeploy with deployer as INDEX_PUBLISHER, publish rebase level.
 *
 * Env: PRIVATE_KEY, RWA_TOKEN_REGISTRY, RWA_TOKEN_FACTORY, REBASE_INDEX_VALUE (default 1885856)
 *      OWNER (default OMNL multisig), COMPLIANCE_ADMIN (default Gnosis Safe admin)
 *      INDEX_PUBLISHER (default deployer), RWA_METHODOLOGY_HASH
 */
contract RedeployLiXauPublisher138 is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address registryAddr = vm.envAddress("RWA_TOKEN_REGISTRY");
        address factoryAddr = vm.envAddress("RWA_TOKEN_FACTORY");
        address owner = vm.envOr("OWNER", vm.envOr("OMNL_COMPLIANCE_MULTISIG", deployer));
        address compliance = vm.envOr("COMPLIANCE_ADMIN", vm.envOr("OMNL_GNOSIS_SAFE_ADMIN", deployer));
        address publisher = vm.envOr("INDEX_PUBLISHER", deployer);
        uint256 rebaseValue = vm.envOr("REBASE_INDEX_VALUE", uint256(1_885_856));
        bytes32 methodologyHash = vm.parseBytes32(vm.envOr("RWA_METHODOLOGY_HASH", string("0x6b6e599d0ba31d048b49302e263a12f0c59502f67a35100ad5c65b503b1d4b82")));

        RWATokenRegistry registry = RWATokenRegistry(registryAddr);
        RWATokenFactory factory = RWATokenFactory(factoryAddr);

        vm.startBroadcast(pk);

        if (registry.isRegistered("LiXAU")) {
            registry.deactivateIndex("LiXAU");
            console.log("Deactivated LiXAU in registry");
        }

        address token = factory.deployRWAIndex(
            IRWATokenFactory.RWAProductConfig({
                indexTicker: "LiXAU",
                name: "XAU Liquidity Index (M00)",
                symbol: "LiXAU",
                decimals: 6,
                assetClass: "Commodities",
                assetGroup: "Precious Metals",
                instrumentType: "Commodity Index",
                underlyingAsset: "Gold",
                gruLayer: "M00",
                jurisdiction: "International",
                initialOwner: owner,
                complianceAdmin: compliance,
                indexPublisher: publisher,
                initialIndexValue: 1_000_000,
                initialSupply: 0,
                methodologyDocumentHash: methodologyHash,
                registerInUniversalAssetRegistry: false
            })
        );

        RWAToken(token).updateIndexValue(rebaseValue);

        vm.stopBroadcast();

        console.log("LiXAU redeployed", token);
        console.log("indexValue", RWAToken(token).indexValue());
        console.log("indexPublisher", publisher);
    }
}
