// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract VibeToken is ERC20, Ownable {
    address public treasury;
    uint256 public taxRate = 5; // 5%

    constructor(address _treasury) ERC20("VibeWork Agent Token", "VIBE") Ownable(msg.sender) {
        treasury = _treasury;
        _mint(msg.sender, 1_000_000_000 * 10 ** decimals()); // 1 Billion tokens
    }

    function setTreasury(address _newTreasury) external onlyOwner {
        treasury = _newTreasury;
    }

    function setTaxRate(uint256 _newRate) external onlyOwner {
        require(_newRate <= 20, "Tax too high"); // Max 20%
        taxRate = _newRate;
    }

    // Override the core transfer logic to implement the Bot Extraction Tax
    function _update(address from, address to, uint256 value) internal virtual override {
        // Bypass tax for mints/burns and owner transfers (initial liquidity provision)
        if (from == address(0) || to == address(0) || from == owner() || to == owner() || taxRate == 0) {
            super._update(from, to, value);
            return;
        }

        // Calculate tax
        uint256 taxAmount = (value * taxRate) / 100;
        uint256 sendAmount = value - taxAmount;

        // Route tax directly to Treasury wallet
        if (taxAmount > 0) {
            super._update(from, treasury, taxAmount);
        }
        
        // Send the rest to the recipient
        super._update(from, to, sendAmount);
    }
}
