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

import {Script, console2} from "forge-std/Script.sol";
import {ReserveCommitmentStore} from "../../contracts/hybx-omnl/ReserveCommitmentStore.sol";

/// @notice Deploy ReserveCommitmentStore v2 (notary gate ABI), migrate commitment from legacy store.
contract MigrateOMNLReserveStoreV2 is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address admin = vm.envOr("OMNL_WEB3_ADMIN", vm.addr(pk));
        address legacyStore = vm.envAddress("OMNL_RESERVE_STORE_138");
        address notary = vm.envAddress("OMNL_NOTARY_REGISTRY");
        bytes32 lineId = vm.envBytes32("OMNL_HEALTH_LINE_ID");

        bytes32 jurId = keccak256(bytes(vm.envOr("OMNL_JURISDICTION_ID", string("ID"))));
        bytes32 matrixId = keccak256(bytes(vm.envOr("OMNL_MATRIX_CONTROL_ID", string("ID-OMNL-001"))));
        bool requireNotary = vm.envOr("OMNL_REQUIRE_NOTARIZED_RESERVE", false);
        uint256 attTh = vm.envOr("OMNL_RESERVE_ATTESTATION_THRESHOLD", uint256(3));

        ReserveCommitmentStore legacy = ReserveCommitmentStore(legacyStore);
        ReserveCommitmentStore.Commitment memory c = legacy.getCommitment(lineId);
        address mirror = legacy.mirrorReceiver();

        vm.startBroadcast(pk);

        ReserveCommitmentStore storeV2 = new ReserveCommitmentStore(admin);
        storeV2.configureNotaryGate(notary, requireNotary, jurId, matrixId);
        storeV2.setAttestationThreshold(attTh);
        if (mirror != address(0)) {
            storeV2.setMirrorReceiver(mirror);
        }
        storeV2.commitReserve(lineId, c.R, c.validUntil, c.evidenceHash, c.merkleRoot);

        vm.stopBroadcast();

        console2.log("ReserveCommitmentStoreV2", address(storeV2));
        console2.log("legacyStore", legacyStore);
        console2.log("notaryGate", notary);
        console2.log("migratedVersion", storeV2.getCommitment(lineId).version);
    }
}
