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

import {Script, console} from "forge-std/Script.sol";

interface IOraclePriceFeedSync {
    function updatePriceFeed(address asset) external;
    function aggregators(address asset) external view returns (address);
}

/// @notice Push wired Chain 138 aggregators into ReserveSystem via OraclePriceFeed (fixes getPoolPriceOrOracle).
contract SyncChain138ReserveFromOracle is Script {
    address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address internal constant CUSDT = 0x93E66202A11B1772E55407B32B44e5Cd8eda7f22;
    address internal constant CUSDC = 0xf22258f57794CC8E06237084b353Ab30fFfa640b;
    address internal constant USDT = 0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1;
    address internal constant USDC = 0x71D6687F38b93CCad569Fa6352c876eea967201b;

    function run() external {
        require(block.chainid == 138, "ChainID 138 only");

        uint256 pk = vm.envUint("PRIVATE_KEY");
        address opf = vm.envAddress("ORACLE_PRICE_FEED");

        address[] memory assets = new address[](5);
        assets[0] = WETH9;
        assets[1] = CUSDT;
        assets[2] = CUSDC;
        assets[3] = USDT;
        assets[4] = USDC;

        console.log("=== Sync ReserveSystem from OraclePriceFeed ===");
        console.log("OraclePriceFeed:", opf);

        vm.startBroadcast(pk);

        IOraclePriceFeedSync feed = IOraclePriceFeedSync(opf);
        for (uint256 i = 0; i < assets.length; i++) {
            address asset = assets[i];
            if (feed.aggregators(asset) == address(0)) {
                console.log("SKIP (no aggregator):", asset);
                continue;
            }
            try feed.updatePriceFeed(asset) {
                console.log("Updated:", asset);
            } catch {
                console.log("WARN updatePriceFeed failed:", asset);
            }
        }

        vm.stopBroadcast();
        console.log("=== Done ===");
    }
}
