// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ECOTERRA2 {
    string public name = "ECOTERRA 2.0";
    string public symbol = "ET2";
    uint256 public totalSupply = 1000000000 * 10**18;
    uint8 public decimals = 18;

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    address public owner;
    address public miningPool;

    uint256 public buyTaxRate = 2;
    uint256 public sellTaxRate = 4;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    constructor() {
        owner = msg.sender;
        balanceOf[msg.sender] = totalSupply;
        emit Transfer(address(0), msg.sender, totalSupply);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the contract owner can perform this action");
        _;
    }

    function setMiningPool(address _miningPool) external onlyOwner {
        miningPool = _miningPool;
    }

    function setBuyTaxRate(uint256 _buyTaxRate) external onlyOwner {
        require(_buyTaxRate <= 100, "Tax rate must be less than or equal to 100");
        buyTaxRate = _buyTaxRate;
    }

    function transfer(address to, uint256 value) external returns (bool) {
        require(to != address(0), "Cannot transfer to the zero address");
        require(value > 0, "Transfer value must be greater than zero");
        require(balanceOf[msg.sender] >= value, "Insufficient balance");

        uint256 taxAmount = calculateTax(value, buyTaxRate);
        uint256 transferAmount = value - taxAmount;

        balanceOf[msg.sender] -= value;
        balanceOf[to] += transferAmount;
        balanceOf[miningPool] += taxAmount;

        emit Transfer(msg.sender, to, transferAmount);
        emit Transfer(msg.sender, miningPool, taxAmount);

        return true;
    }

    function approve(address spender, uint256 value) external returns (bool) {
        require(spender != address(0), "Cannot approve to the zero address");
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }

    function transferFrom(address from, address to, uint256 value) external returns (bool) {
        require(to != address(0), "Cannot transfer to the zero address");
        require(value > 0, "Transfer value must be greater than zero");
        require(balanceOf[from] >= value, "Insufficient balance");
        require(allowance[from][msg.sender] >= value, "Insufficient allowance");

        uint256 taxAmount = calculateTax(value, buyTaxRate);
        uint256 transferAmount = value - taxAmount;

        balanceOf[from] -= value;
        balanceOf[to] += transferAmount;
        balanceOf[miningPool] += taxAmount;

        allowance[from][msg.sender] -= value;

        emit Transfer(from, to, transferAmount);
        emit Transfer(from, miningPool, taxAmount);

        return true;
    }

    function calculateTax(uint256 value, uint256 taxRate) internal pure returns (uint256) {
        return (value * taxRate) / 100;
    }
}