pragma solidity 0.8.20;

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

import {ERC20Permit} from "@openzeppelin-contracts/token/ERC20/extensions/ERC20Permit.sol";
import {SafeERC20} from "@openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";

abstract contract ERC20PermitTest is Test {
    using SafeERC20 for ERC20Permit;

    SigUtils sigUtils;
    ERC20Permit permitToken;

    address owner;
    address spender;
    uint256 permitAmount;
    uint256 maxPermitAmount;

    uint256 constant OWNER_PRIVATE_KEY   = 0xA11CE;
    uint256 constant SPENDER_PRIVATE_KEY = 0xB0B;

    function initializePermit(
        address token,
        uint256 amount,
        uint256 maxAmount
    ) public {
        permitAmount = amount;
        maxPermitAmount = maxAmount;
        permitToken = ERC20Permit(token);
        sigUtils = new SigUtils(permitToken.DOMAIN_SEPARATOR());

        owner = vm.addr(OWNER_PRIVATE_KEY);
        spender = vm.addr(SPENDER_PRIVATE_KEY);
    }

    function test_permit_initialNonceIsZero() public view {
        assertEq(permitToken.nonces(owner), 0);
        assertEq(permitToken.nonces(spender), 0);
    }

    function test_permit_acceptsOwnerSignature() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: 0,
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );

        assertEq(permitToken.allowance(owner, spender), permitAmount);
        assertEq(permitToken.nonces(owner), 1);
    }

    function test_permit_expiredPermit() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: permitToken.nonces(owner),
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        vm.warp(1 days + 1 seconds); // fast forward one second past the deadline

        vm.expectRevert();
        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );
    }

    function test_permit_invalidSigner() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: permitToken.nonces(owner),
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(SPENDER_PRIVATE_KEY, digest); // spender signs owner's approval

        vm.expectRevert();
        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );
    }

    function test_permit_invalidNonce() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: 1, // owner nonce stored on-chain is 0
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        vm.expectRevert();
        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );
    }

    function test_permit_signatureReplay() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: 0,
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );

        vm.expectRevert();
        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );
    }

    function test_permit_transferFromLimitedPermit() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: 0,
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );

        vm.prank(spender);
        permitToken.safeTransferFrom(owner, spender, permitAmount);

        assertEq(permitToken.balanceOf(owner), 0);
        assertEq(permitToken.balanceOf(spender), permitAmount);
        assertEq(permitToken.allowance(owner, spender), 0);
    }

    function test_permit_transferFromMaxPermit() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: maxPermitAmount,
            nonce: 0,
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );

        vm.prank(spender);
        permitToken.safeTransferFrom(owner, spender, permitAmount);

        assertEq(permitToken.balanceOf(owner), 0);
        assertEq(permitToken.balanceOf(spender), permitAmount);
    }

    function test_permit_invalidAllowance() public {
        _mintPermitTokens(owner, permitAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
           owner: owner,
           spender: spender,
           value: 1,
           nonce: 0,
           deadline: 1 days
       });

       bytes32 digest = sigUtils.getTypedDataHash(permit);

       (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

       permitToken.permit(
           permit.owner,
           permit.spender,
           permit.value,
           permit.deadline,
           v,
           r,
           s
       );

       vm.expectRevert();
       vm.prank(spender);
       assertEq(permitToken.transferFrom(owner, spender, permitAmount), false);
       assertEq(permitToken.balanceOf(owner), permitAmount);
       assertEq(permitToken.balanceOf(spender), 0);
    }

    function test_permit_invalidBalance() public {
        uint256 smallAmount = 2;
        _mintPermitTokens(owner, smallAmount);
        SigUtils.Permit memory permit = SigUtils.Permit({
            owner: owner,
            spender: spender,
            value: permitAmount,
            nonce: 0,
            deadline: 1 days
        });

        bytes32 digest = sigUtils.getTypedDataHash(permit);

        (uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PRIVATE_KEY, digest);

        permitToken.permit(
            permit.owner,
            permit.spender,
            permit.value,
            permit.deadline,
            v,
            r,
            s
        );

        vm.expectRevert();
        vm.prank(spender);
        assertEq(permitToken.transferFrom(owner, spender, permitAmount), false);
        assertEq(permitToken.balanceOf(owner), smallAmount);
        assertEq(permitToken.balanceOf(spender), 0);
    }

    function _mintPermitTokens(address who, uint256 permitAmount) internal virtual;
}
