pragma solidity 0.8.20;

import {Test} from "forge-std/Test.sol";

import {GhostAuthority} from "../../src/GhostAuthority.sol";
import {GhostTreasury} from "../../src/Treasury.sol";
import {GhostBondingCalculator} from "../../src/StandardBondingCalculator.sol";

import {ITreasury} from "../../src/interfaces/ITreasury.sol";
import {IERC20} from "@openzeppelin-contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";

import {IWETH} from "@uniswap-v2-periphery-1.1.0-beta.0/interfaces/IWETH.sol";

contract GhostTreasuryRedemptionTest is Test {
    using SafeERC20 for IERC20;

    address public constant OWNER    = 0x0000000000000000000000000000000000000001;
    address public constant GOVERNOR = 0x0000000000000000000000000000000000000002;
    address public constant GUARDIAN = 0x0000000000000000000000000000000000000003;

    uint256 public constant AMOUNT = 69 * 1e18;
    address public constant DAI    = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address public constant PAIR   = 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11;
    address public constant ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    address public constant WETH   = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    GhostTreasury treasury;
    GhostAuthority authority;
    GhostBondingCalculator calculator;

    function setUp() public {
        vm.startPrank(OWNER);
        authority = new GhostAuthority(
            GOVERNOR,
            GUARDIAN,
            OWNER,
            OWNER
        );
        treasury = new GhostTreasury(DAI, 69, address(authority));
        calculator = new GhostBondingCalculator(DAI, 4000, 1);
        vm.stopPrank();

        vm.startPrank(GOVERNOR);
        treasury.enable(ITreasury.STATUS.RESERVETOKEN, WETH, address(calculator));
        treasury.enable(ITreasury.STATUS.LIQUIDITYTOKEN, PAIR, address(calculator));
        vm.stopPrank();
    }

    function test_mainnet_redemptionWorksCorrectly() public {
        vm.deal(OWNER, AMOUNT);
        vm.startPrank(OWNER);
        IWETH(WETH).deposit{value: AMOUNT}();
        IERC20(WETH).safeTransfer(address(treasury), AMOUNT);
        assertEq(IERC20(WETH).balanceOf(address(treasury)), AMOUNT);
        vm.stopPrank();

        assertEq(treasury.totalReserves(), 0);

        vm.prank(GOVERNOR);
        treasury.auditReserves();

        uint256 prevTotalReserves = treasury.totalReserves();
        assertEq(prevTotalReserves > 0, true);
        assertEq(IERC20(PAIR).balanceOf(address(treasury)), 0);
        assertEq(IERC20(PAIR).balanceOf(address(treasury)), 0);

        vm.prank(GOVERNOR);
        treasury.redeemReserve(ROUTER, AMOUNT);

        assertEq(IERC20(DAI).balanceOf(address(treasury)), 0);
        assertEq(IERC20(WETH).balanceOf(address(treasury)), 0);
        assertEq(IERC20(PAIR).balanceOf(address(treasury)) > 0, true);

        uint256 liquidity = IERC20(PAIR).balanceOf(address(treasury));
        vm.prank(GOVERNOR);
        treasury.forfeitReserves(ROUTER, liquidity, false);

        assertEq(IERC20(DAI).balanceOf(address(treasury)) > 0, true);
        assertEq(IERC20(WETH).balanceOf(address(treasury)) > 0, true);
        assertEq(IERC20(PAIR).balanceOf(address(treasury)), 0);
    }
}
