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

import {Test} from "forge-std/Test.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {Chain138MainnetCheckpoint} from "../../contracts/mainnet-checkpoint/Chain138MainnetCheckpoint.sol";
import {CheckpointHubConfig} from "../../contracts/mainnet-checkpoint/libraries/CheckpointHubConfig.sol";
import {ExtensionIds} from "../../contracts/mainnet-checkpoint/libraries/ExtensionIds.sol";
import {MetricsExtension} from "../../contracts/mainnet-checkpoint/extensions/MetricsExtension.sol";

contract CheckpointHubConfigTest is Test {
    Chain138MainnetCheckpoint hub;
    address admin = address(0xA11CE);

    function setUp() public {
        Chain138MainnetCheckpoint impl = new Chain138MainnetCheckpoint();
        bytes memory initData = abi.encodeCall(
            Chain138MainnetCheckpoint.initialize,
            (admin, address(0x1001), uint64(999), address(0))
        );
        hub = Chain138MainnetCheckpoint(address(new ERC1967Proxy(address(impl), initData)));
    }

    function testApplyConfigUpdatesBatchSize() public {
        CheckpointHubConfig.HubConfig memory cfg = CheckpointHubConfig.mainnetDefaults();
        cfg.batchSize = 25;
        cfg.maxBatchWaitSeconds = 600;
        cfg.ccipRouter = address(0x1001);

        vm.prank(admin);
        hub.applyConfig(cfg);

        (uint16 batchSize, uint32 maxWait,,,,) = hub.getConfig();
        assertEq(batchSize, 25);
        assertEq(maxWait, 600);
    }

    function testSetExtensionActiveToggle() public {
        MetricsExtension metrics = new MetricsExtension();
        vm.startPrank(admin);
        hub.registerExtension(
            ExtensionIds.METRICS,
            address(metrics),
            metrics.HOOK_AFTER_SUBMIT()
        );
        hub.setExtensionActive(ExtensionIds.METRICS, false);
        vm.stopPrank();
        (, , bool active) = hub.getExtension(ExtensionIds.METRICS);
        assertFalse(active);
    }

    function testEnforcePreviousBatchIdCanDisable() public {
        CheckpointHubConfig.HubConfig memory cfg = CheckpointHubConfig.mainnetDefaults();
        cfg.enforcePreviousBatchId = false;
        vm.prank(admin);
        hub.applyConfig(cfg);
        CheckpointHubConfig.HubConfig memory full = hub.getFullConfig();
        assertFalse(full.enforcePreviousBatchId);
    }
}
