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

import {Test, console} from "forge-std/Test.sol";
import {Aggregator} from "../contracts/oracle/Aggregator.sol";

contract AggregatorTest is Test {
    Aggregator public aggregator;
    address public admin = address(1);
    address public transmitter = address(2);
    
    function setUp() public {
        vm.prank(admin);
        aggregator = new Aggregator(
            "ETH/USD Price Feed",
            admin,
            60,  // heartbeat: 60 seconds
            50   // deviationThreshold: 0.5% (50 basis points)
        );
        
        vm.prank(admin);
        aggregator.addTransmitter(transmitter);
    }
    
    function testUpdateAnswer() public {
        vm.prank(transmitter);
        aggregator.updateAnswer(2000e8);
        
        (uint80 roundId, int256 answer, , , ) = aggregator.latestRoundData();
        assertEq(roundId, 1);
        assertEq(answer, 2000e8);
    }
    
    function testHeartbeat() public {
        vm.prank(transmitter);
        aggregator.updateAnswer(2000e8);
        
        // Fast forward time
        vm.warp(block.timestamp + 61);
        
        vm.prank(transmitter);
        aggregator.updateAnswer(2001e8);
        
        (uint80 roundId, , , , ) = aggregator.latestRoundData();
        assertEq(roundId, 2);
    }
    
    function testDeviationThreshold() public {
        vm.prank(transmitter);
        aggregator.updateAnswer(2000e8);
        
        // 1% deviation should trigger new round
        vm.prank(transmitter);
        aggregator.updateAnswer(2020e8);
        
        (uint80 roundId, , , , ) = aggregator.latestRoundData();
        assertEq(roundId, 2);
    }
    
    function testOnlyTransmitter() public {
        vm.expectRevert("Aggregator: only transmitter");
        aggregator.updateAnswer(2000e8);
    }
    
    function testAddTransmitter() public {
        address newTransmitter = address(3);
        
        vm.prank(admin);
        aggregator.addTransmitter(newTransmitter);
        
        assertTrue(aggregator.isTransmitter(newTransmitter));
    }
    
    function testPause() public {
        vm.prank(admin);
        aggregator.pause();
        
        vm.prank(transmitter);
        vm.expectRevert("Aggregator: paused");
        aggregator.updateAnswer(2000e8);
    }
}

