// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MoonToken {
    string public name = "MoonToken";
    string public symbol = "MOON";
    uint8 public decimals = 18;
    uint256 public totalSupply = 1000000000 * 10**uint256(decimals); // 1,000,000,000 tokens

    mapping(address => uint256) private balanceOf;
    mapping(address => mapping(address => uint256)) private allowance;

    address private owner;
    address public devsWallet = 0xd895d3ec2C28b8Bd751A42871B4737C5660D07b2;

    uint256 private constant taxRate = 1000; // 10% tax rate
    uint256 private maxTransactionAmount;
    
    uint256 public ethToTokenRatio = 10000; // 1 Ether = 10,000 tokens

    bool public paused;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event Paused();
    event Unpaused();
    event LiquidityAdded(uint256 ethAmount, uint256 tokenAmount);

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the contract owner can perform this action");
        _;
    }

    modifier whenNotPaused() {
        require(!paused, "Contract is currently paused");
        _;
    }

    constructor() {
        owner = msg.sender;
        balanceOf[msg.sender] = totalSupply;
        maxTransactionAmount = 10000 * 10**uint256(decimals); // 10,000 tokens
    }

    function setDevsWallet(address newDevsWallet) external onlyOwner {
        require(newDevsWallet != address(0), "Invalid devs wallet address");
        devsWallet = newDevsWallet;
    }

    function pause() external onlyOwner whenNotPaused {
        paused = true;
        emit Paused();
    }

    function unpause() external onlyOwner {
        paused = false;
        emit Unpaused();
    }

    function _transfer(address from, address to, uint256 value) internal whenNotPaused {
        require(from != address(0), "Invalid sender address");
        require(to != address(0), "Invalid recipient address");
        require(value > 0, "Invalid amount");

        require(value <= maxTransactionAmount, "Transaction amount exceeds the maximum allowed");

        uint256 taxAmount = value * taxRate / 10000; // Corrected multiplication here
        uint256 transferAmount = value - taxAmount; // Corrected subtraction here

        balanceOf[from] = balanceOf[from] - value; // Corrected subtraction here
        balanceOf[to] = balanceOf[to] + transferAmount; // Corrected addition here
        balanceOf[devsWallet] = balanceOf[devsWallet] + taxAmount; // Corrected addition here

        emit Transfer(from, to, transferAmount);
        emit Transfer(from, devsWallet, taxAmount);
    }

    function transfer(address to, uint256 value) external returns (bool) {
        _transfer(msg.sender, to, value);
        return true;
    }

    function approve(address spender, uint256 value) external returns (bool) {
        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(value <= allowance[from][msg.sender], "Allowance exceeded");
        allowance[from][msg.sender] = allowance[from][msg.sender] - value; // Corrected subtraction here
        _transfer(from, to, value);
        return true;
    }
    
    function addLiquidity() external payable whenNotPaused {
        uint256 ethAmount = msg.value;
        require(ethAmount > 0, "You must send some ETH to add liquidity");
        uint256 tokenAmount = ethAmount * ethToTokenRatio;
        
        require(tokenAmount <= balanceOf[msg.sender], "Insufficient balance to add liquidity");
        balanceOf[msg.sender] = balanceOf[msg.sender] - tokenAmount; // Corrected subtraction here
        totalSupply = totalSupply + tokenAmount; // Corrected addition here
        
        emit Transfer(msg.sender, address(0), tokenAmount); // Burn tokens
        emit LiquidityAdded(ethAmount, tokenAmount);
    }
    
    receive() external payable {} // Allow receiving ETH
}