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

import {Test, console} from "forge-std/Test.sol";
import "../../../../contracts/bridge/trustless/integration/StablecoinPegManager.sol";
import "../../../../contracts/bridge/trustless/integration/CommodityPegManager.sol";
import "../../../../contracts/bridge/trustless/integration/ISOCurrencyManager.sol";
import "../../../../contracts/reserve/ReserveSystem.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MockERC20 is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        _mint(msg.sender, 1000000 ether);
    }
}

contract PegManagementIntegrationTest is Test {
    ReserveSystem public reserveSystem;
    StablecoinPegManager public stablecoinPegManager;
    CommodityPegManager public commodityPegManager;
    ISOCurrencyManager public isoCurrencyManager;
    
    MockERC20 public usdt;
    MockERC20 public usdc;
    MockERC20 public weth;
    MockERC20 public xau;
    MockERC20 public xag;

    address public deployer = address(0xDE0001);

    function setUp() public {
        vm.startPrank(deployer);

        usdt = new MockERC20("Tether USD", "USDT");
        usdc = new MockERC20("USD Coin", "USDC");
        weth = new MockERC20("Wrapped Ether", "WETH");
        xau = new MockERC20("Gold", "XAU");
        xag = new MockERC20("Silver", "XAG");

        reserveSystem = new ReserveSystem(deployer);
        reserveSystem.grantRole(keccak256("PRICE_FEED_ROLE"), deployer);

        // Set prices
        reserveSystem.updatePriceFeed(address(usdt), 1e18, block.timestamp);
        reserveSystem.updatePriceFeed(address(usdc), 1e18, block.timestamp);
        reserveSystem.updatePriceFeed(address(weth), 1e18, block.timestamp);
        reserveSystem.updatePriceFeed(address(xau), 2000e18, block.timestamp);
        reserveSystem.updatePriceFeed(address(xag), 25e18, block.timestamp);

        // Deploy peg managers
        stablecoinPegManager = new StablecoinPegManager(address(reserveSystem));
        stablecoinPegManager.registerUSDStablecoin(address(usdt));
        stablecoinPegManager.registerUSDStablecoin(address(usdc));
        stablecoinPegManager.registerWETH(address(weth));

        commodityPegManager = new CommodityPegManager(address(reserveSystem));
        commodityPegManager.setXAUAddress(address(xau));
        commodityPegManager.registerCommodity(address(xag), "XAG", 80e18);

        isoCurrencyManager = new ISOCurrencyManager(address(reserveSystem));
        isoCurrencyManager.setXAUAddress(address(xau));
        isoCurrencyManager.registerCurrency("USD", address(usdt), 2000e18);
        isoCurrencyManager.registerCurrency("EUR", address(0), 1800e18);
        isoCurrencyManager.registerCurrency("GBP", address(0), 1500e18);

        vm.stopPrank();
    }

    function testStablecoinPeg_OnPeg() public view {
        (bool usdtMaintained, int256 usdtDeviation) = stablecoinPegManager.checkUSDpeg(address(usdt));
        assertTrue(usdtMaintained);
        assertEq(usdtDeviation, 0);

        (bool usdcMaintained, int256 usdcDeviation) = stablecoinPegManager.checkUSDpeg(address(usdc));
        assertTrue(usdcMaintained);
        assertEq(usdcDeviation, 0);
    }

    function testStablecoinPeg_Deviation() public {
        // Set price to $1.01 (1% above peg) - requires PRICE_FEED_ROLE
        vm.prank(deployer);
        reserveSystem.updatePriceFeed(address(usdt), 1.01e18, block.timestamp);
        
        (bool isMaintained, int256 deviationBps) = stablecoinPegManager.checkUSDpeg(address(usdt));
        
        // Should not be maintained (deviation > 0.5% threshold)
        assertFalse(isMaintained);
        assertEq(deviationBps, 100); // 1% = 100 bps
    }

    function testCommodityPeg_ViaXAU() public view {
        (bool isMaintained, int256 deviationBps) = commodityPegManager.checkCommodityPeg(address(xag));
        
        // XAG should be pegged via XAU
        // XAU = $2000, XAG rate = 80, so XAG should be $25
        // Price is set at $25, so should be maintained
        assertTrue(isMaintained);
    }

    function testISOCurrency_Triangulation() public view {
        // Convert 2000 USD to EUR via XAU
        uint256 usdAmount = 2000 ether;
        uint256 eurAmount = isoCurrencyManager.convertViaXAU("USD", "EUR", usdAmount);
        
        // 2000 USD = 1 oz XAU = 1800 EUR
        assertApproxEqRel(eurAmount, 1800 ether, 0.01e18);
    }

    function testISOCurrency_MultipleConversions() public view {
        // USD -> EUR -> GBP via XAU
        uint256 usdAmount = 2000 ether;
        uint256 eurAmount = isoCurrencyManager.convertViaXAU("USD", "EUR", usdAmount);
        uint256 gbpAmount = isoCurrencyManager.convertViaXAU("EUR", "GBP", eurAmount);
        
        // 2000 USD = 1800 EUR = 1500 GBP (via XAU)
        assertApproxEqRel(gbpAmount, 1500 ether, 0.01e18);
    }

    function testPegThreshold_Configuration() public {
        uint256 newThreshold = 100; // 1%
        
        vm.prank(deployer);
        stablecoinPegManager.setUSDPegThreshold(newThreshold);
        
        assertEq(stablecoinPegManager.usdPegThresholdBps(), newThreshold);
    }

    function testGetAllSupportedCurrencies() public view {
        string[] memory currencies = isoCurrencyManager.getAllSupportedCurrencies();
        assertGe(currencies.length, 3); // USD, EUR, GBP
    }

    function testCurrencyRate_Calculation() public view {
        uint256 rate = isoCurrencyManager.getCurrencyRate("USD", "EUR");
        
        // Rate should be (1800 / 2000) * 1e18 = 0.9e18
        assertApproxEqRel(rate, 0.9e18, 0.01e18);
    }
}

