// 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 reserve store with deployer bootstrap, enable notary gate, migrate commitment, hand DEFAULT_ADMIN to vault Safe.
contract RecoverOMNLReserveVaultSafe is Script {
    bytes32 private constant DEFAULT_ADMIN_ROLE = 0x00;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address vaultSafe = vm.envAddress("OMNL_VAULT_SAFE");
        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"))));
        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 storeV3 = new ReserveCommitmentStore(deployer);
        storeV3.configureNotaryGate(notary, true, jurId, matrixId);
        storeV3.setAttestationThreshold(attTh);
        if (mirror != address(0)) {
            storeV3.setMirrorReceiver(mirror);
        }
        if (c.R != 0) {
            storeV3.commitReserve(lineId, c.R, c.validUntil, c.evidenceHash, c.merkleRoot);
        }
        storeV3.grantRole(DEFAULT_ADMIN_ROLE, vaultSafe);
        storeV3.revokeRole(DEFAULT_ADMIN_ROLE, deployer);

        vm.stopBroadcast();

        console2.log("ReserveCommitmentStoreV3", address(storeV3));
        console2.log("vaultSafe", vaultSafe);
        console2.log("requireNotarizedEvidence", storeV3.requireNotarizedEvidence());
        console2.log("legacyStore", legacyStore);
    }
}
