pragma solidity 0.8.20;

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

import "../../src/FatsoERC20.sol";
import "../../src/GhostAuthority.sol";
import "../../src/Treasury.sol";
import "../../src/StandardBondingCalculator.sol";
import "../../src/mocks/ERC20Mock.sol";

contract GhostTreasuryCoefficienBiggerTest is Test {
    address public constant owner    = 0x0000000000000000000000000000000000000001;
    address public constant governor = 0x0000000000000000000000000000000000000002;
    address public constant guardian = 0x0000000000000000000000000000000000000003;
    address public constant other    = 0x0000000000000000000000000000000000000004;
    address public constant alice    = 0x0000000000000000000000000000000000000005;
    address public constant bob      = 0x0000000000000000000000000000000000000006;

    uint256 public constant amount = 69 * 1e18;

    Fatso ftso;
    ERC20Mock reserve;
    ERC20Mock liquidity;
    GhostTreasury treasury;
    GhostAuthority authority;
    GhostBondingCalculator calculator;

    function setUp() public {
        vm.startPrank(owner);
        authority = new GhostAuthority(
            governor,
            guardian,
            owner,
            owner
        );
        reserve = new ERC20Mock("Reserve Token", "RET");
        liquidity = new ERC20Mock("Liquidity Token", "LDT");
        ftso = new Fatso(address(authority), "Fatso", "FTSO");
        treasury = new GhostTreasury(address(ftso), 69, address(authority));
        calculator = new GhostBondingCalculator(address(ftso), 20, 1);
        vm.stopPrank();

        vm.prank(governor);
        authority.pushVault(address(treasury));

        vm.startPrank(alice);
        reserve.mint(alice, amount);
        reserve.approve(address(treasury), type(uint256).max);
        ftso.approve(address(treasury), type(uint256).max);
        vm.stopPrank();
    }

    function test_auditTreasuryReservesWithCoefficient() public {
        vm.startPrank(governor);
        treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
        treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
        vm.stopPrank();

        assertEq(treasury.totalReserves(), 0);

        uint256 tokenValue = treasury.tokenValue(address(reserve), amount);
        vm.prank(alice);
        IERC20(reserve).transfer(address(treasury), amount);

        assertEq(treasury.totalReserves(), 0);

        vm.prank(governor);
        treasury.auditReserves();
        assertEq(treasury.totalReserves(), tokenValue);

        uint256 coefficient = treasury.originalCoefficient();
        uint256 decimals = IERC20Metadata(address(reserve)).decimals() + 9;
        uint256 convertedReserves = treasury.totalReserves() * 10**decimals / coefficient;
        uint256 convertedEps = convertedReserves * 1e5 / 1e12; // .000001%

        assertEq(convertedReserves + convertedEps >= convertedReserves, true);
        assertEq(convertedReserves - convertedEps <= convertedReserves, true);

        assertEq(treasury.excessReserves(), tokenValue);
    }

    function test_originalCoefficientCorrect() public {
        vm.startPrank(governor);
        treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
        treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
        vm.stopPrank();

        uint256 estimation = 20 * 1e18;
        uint256 estimationEps = estimation * 1e5 / 1e12; // .000001%

        assertEq(estimation + estimationEps >= treasury.originalCoefficient(), true);
        assertEq(estimation - estimationEps <= treasury.originalCoefficient(), true);
    }
}
