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

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

interface IDODOPMMIntegrationWire {
    function setReserveSystem(address reserveSystem_) external;
    function reserveSystem() external view returns (address);
}

/// @notice Deploy conversion adapter and point Stack A DODOPMMIntegration at it (live ReserveSystem formula fix).
contract WireChain138ReserveConversionAdapter is Script {
    address internal constant DEFAULT_RESERVE = 0x607e97cD626f209facfE48c1464815DDE15B5093;
    address internal constant DEFAULT_DODO = 0x86ADA6Ef91A3B450F89f2b751e93B1b7A3218895;

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

        uint256 pk = vm.envUint("PRIVATE_KEY");
        address reserve = vm.envOr("RESERVE_SYSTEM", DEFAULT_RESERVE);
        address dodo = vm.envOr("DODO_PMM_INTEGRATION_ADDRESS", DEFAULT_DODO);

        console.log("=== Wire ReserveConversionPriceAdapter ===");
        console.log("ReserveSystem:", reserve);
        console.log("DODOPMMIntegration:", dodo);
        console.log("Current reserveSystem:", IDODOPMMIntegrationWire(dodo).reserveSystem());

        vm.startBroadcast(pk);

        ReserveConversionPriceAdapter adapter = new ReserveConversionPriceAdapter(reserve);
        console.log("Adapter deployed:", address(adapter));
        IDODOPMMIntegrationWire(dodo).setReserveSystem(address(adapter));

        vm.stopBroadcast();

        console.log("Wired reserveSystem:", IDODOPMMIntegrationWire(dodo).reserveSystem());
        console.log("=== Done ===");
    }
}
