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

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

contract ContractDeploymentTest is Test {
    address public admin;
    address public transmitter;
    address public linkToken;
    
    function setUp() public {
        admin = address(this);
        transmitter = address(0x123);
        linkToken = address(0x456);
    }
    
    function testDeployAggregator() public {
        Aggregator aggregator = new Aggregator(
            "ETH/USD",
            admin,
            60,
            50
        );
        
        assertEq(aggregator.description(), "ETH/USD");
        assertEq(aggregator.admin(), admin);
        assertEq(aggregator.heartbeat(), 60);
        assertEq(aggregator.deviationThreshold(), 50);
    }
    
    function testDeployCCIPRouter() public {
        uint256 baseFee = 1 ether;
        uint256 dataFeePerByte = 1000;
        CCIPRouter router = new CCIPRouter(linkToken, baseFee, dataFeePerByte);
        
        assertEq(router.admin(), admin);
        assertEq(router.baseFee(), baseFee);
        assertEq(router.dataFeePerByte(), dataFeePerByte);
        assertEq(router.feeToken(), linkToken);
    }
    
    function testDeployCCIPSender() public {
        uint256 baseFee = 1 ether;
        uint256 dataFeePerByte = 1000;
        address router = address(new CCIPRouter(linkToken, baseFee, dataFeePerByte));
        address aggregator = address(new Aggregator("ETH/USD", admin, 60, 50));
        
        CCIPSender sender = new CCIPSender(router, aggregator, linkToken);
        
        assertEq(address(sender.ccipRouter()), router);
        assertEq(sender.oracleAggregator(), aggregator);
        assertEq(sender.feeToken(), linkToken);
    }
    
    function testDeployCCIPReceiver() public {
        uint256 baseFee = 1 ether;
        uint256 dataFeePerByte = 1000;
        address router = address(new CCIPRouter(linkToken, baseFee, dataFeePerByte));
        address aggregator = address(new Aggregator("ETH/USD", admin, 60, 50));
        
        CCIPReceiver receiver = new CCIPReceiver(router, aggregator);
        
        assertEq(address(receiver.router()), router);
        assertEq(receiver.oracleAggregator(), aggregator);
    }
    
    function testFullDeploymentFlow() public {
        // Deploy aggregator
        Aggregator aggregator = new Aggregator("ETH/USD", admin, 60, 50);
        
        // Deploy CCIP router
        uint256 baseFee = 1 ether;
        uint256 dataFeePerByte = 1000;
        CCIPRouter router = new CCIPRouter(linkToken, baseFee, dataFeePerByte);
        
        // Deploy CCIP sender
        CCIPSender sender = new CCIPSender(address(router), address(aggregator), linkToken);
        
        // Deploy CCIP receiver
        CCIPReceiver receiver = new CCIPReceiver(address(router), address(aggregator));
        
        // Verify all contracts deployed
        assertTrue(address(aggregator) != address(0));
        assertTrue(address(router) != address(0));
        assertTrue(address(sender) != address(0));
        assertTrue(address(receiver) != address(0));
    }
    
    function testDeploymentWithConfiguration() public {
        // Deploy with custom configuration
        Aggregator aggregator = new Aggregator("BTC/USD", admin, 120, 100);
        
        // Add transmitter
        aggregator.addTransmitter(transmitter);
        
        // Verify configuration
        assertEq(aggregator.description(), "BTC/USD");
        assertEq(aggregator.heartbeat(), 120);
        assertEq(aggregator.deviationThreshold(), 100);
        assertTrue(aggregator.isTransmitter(transmitter));
    }
}

