// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4; 

// GM, Chad - unchainedchad.eth
// ERC20 token for the $GM CHAD Economic System & Governance Protocol
// "Behind this mask there is more than just flesh. Beneath this mask there is..."
// https://www.gmchad.com

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// @dev - unchainedchad.eth, deploys contract and sets teamwallet as owner
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor() {
        _transferOwnership(_msgSender());
    }

    function owner() public view virtual returns (address) {
        return _owner;
    }

    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// @dev Interface of the ERC20 standard as defined in the EIP.
interface IERC20 {

    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}


// @dev Interface for the optional metadata functions from the ERC20 standard.
interface IERC20Metadata is IERC20 {

    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
}

contract GMchad is Ownable, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;
    mapping (address => bool) private _isExcludedFromFee;
    mapping(address => mapping(address => uint256)) private _allowances;

    string constant private _name = "GMchad";
    string constant private _symbol = "GMCHAD";
    uint8  constant private _decimal = 10;
    uint256 private _totalSupply = 500000000 * (10 ** _decimal); // Total Supply 500 million
    uint256 constant public _taxBurn = 4; // 0.4% 
    uint256 constant public _taxLiquidity = 0; // 0%
    address public teamWallet;
    uint256 public toBurnAmount = 0;

    event teamWalletChanged(address oldWalletAddress, address newWalletAddress);
    event feeCollected(address teamWallet, uint256 amount);
    event excludingAddressFromFee(address account);
    event includingAddressInFee(address account);

    modifier onlyTeamWallet() {
        require(teamWallet == _msgSender(), "Caller is not the teamwallet");
        _;
    }

    // @dev sets Initially Team Wallet as Contract Owner
    constructor(address _teamWallet) {
        require(_teamWallet!=address(0), "Cannot set teamwallet as zero address");
        _balances[_msgSender()] = _totalSupply;
        _isExcludedFromFee[_msgSender()] = true;
        _isExcludedFromFee[address(this)] = true;
        _isExcludedFromFee[_teamWallet] = true;
        teamWallet = _teamWallet;  
        emit Transfer(address(0), _msgSender(), _totalSupply);
    }
    
    function name() external view virtual override returns (string memory) {
        return _name;
    }

    function symbol() external view virtual override returns (string memory) {
        return _symbol;
    }
    
    function decimals() external view virtual override returns (uint8) {
        return _decimal;
    }
    
    function totalSupply() external view virtual override returns (uint256) {
        return _totalSupply;
    }
    
    function balanceOf(address account) external view virtual override returns (uint256) {
        return _balances[account];
    }
    
    function collectedFees() external view returns (uint256) {
        return _balances[address(this)];
    }

    function transfer(address recipient, uint256 amount) external virtual override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }
    
    function allowance(address owner, address spender) external view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }
    
    function approve(address spender, uint256 amount) external virtual override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function excludeFromFee(address account) external onlyOwner {
        require(account!=address(0), "Excluding for the zero address");
        _isExcludedFromFee[account] = true;
        emit excludingAddressFromFee(account);
    }

    function isExcludedFromFee(address account) external view returns(bool) {
        return _isExcludedFromFee[account];
    }

    function includeInFee(address account) external onlyOwner {
        require(account!=address(0), "Including for the zero address");
        _isExcludedFromFee[account] = false;
        emit includingAddressInFee(account);
    }

    function collectFees() external onlyOwner {
        uint256 fees = _balances[address(this)];
        _transfer(address(this), teamWallet, _balances[address(this)]);
        emit feeCollected(teamWallet, fees);
    }

    function burnCollectedFees() external onlyTeamWallet {
        require(_balances[teamWallet] >= toBurnAmount, "Does not have the required amount of tokens to burn");
        _transfer(teamWallet, address(0), toBurnAmount);
        _totalSupply -= toBurnAmount;
        toBurnAmount = 0;
        emit feeCollected(address(0), toBurnAmount);
    }

    function updateTeamWallet(address _teamWallet) external onlyOwner {
        require(_teamWallet!=address(0), "Cannot set teamwallet as zero address");
        address oldWallet = teamWallet;
        teamWallet =  _teamWallet;
        _isExcludedFromFee[_teamWallet] = true;
        _isExcludedFromFee[oldWallet] = false;
        emit teamWalletChanged(oldWallet,_teamWallet);
    }
    
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        unchecked {
            _approve(sender, _msgSender(), currentAllowance - amount);
        }

        return true;
    }
    
    function increaseAllowance(address spender, uint256 addedValue) external virtual returns (bool) {
        require(spender!=address(0), "Increasing allowance for zero address");
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
        return true;
    }
    
    function decreaseAllowance(address spender, uint256 subtractedValue) external virtual returns (bool) {
        require(spender!=address(0), "Decreasing allowance for zero address");
        uint256 currentAllowance = _allowances[_msgSender()][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(_msgSender(), spender, currentAllowance - subtractedValue);
        }
        return true;
    }
    
    function _transfer(
        address sender,
        address recipient,
        uint256 amount
    ) internal virtual {
        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[sender] = senderBalance - amount;
        }
        if(_isExcludedFromFee[sender]) {
            unchecked {//condititon to exclude
                _balances[recipient] += amount;
            }
        }else{ 
            unchecked {
                uint256 burnFee =  (amount * _taxBurn) / 1000;
                uint256 tFee = (amount * (_taxBurn + _taxLiquidity)) / 1000;
                amount = amount - tFee;
                _balances[recipient] += amount;
                _balances[address(this)] +=  tFee;
                toBurnAmount += burnFee;
            }
        }
        emit Transfer(sender, recipient, amount);
    }

    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    } 
}


// File contracts/YourTokenDAO.sol

pragma solidity ^0.8.4;

// GM, Chad - unchainedchad.eth
// DAO Smart Contract for the $GM CHAD Governance Protocol
// https://www.gmchad.com

contract YourTokenDAO {
    struct Proposal {
        string title;
        string description;
        uint256 yesVotes;
        address proposer;
        uint256 endTimestamp;
    }

    GMchad public token;
    Proposal[] public proposals;

    // This nested mapping tracks if a user has voted on a particular proposal
    mapping(uint256 => mapping(address => bool)) public hasVotedOnProposal;

    // This mapping tracks if a user has a live proposal
    mapping(address => bool) public hasLiveProposal;

    uint256 public proposalRequirement; // 0.5% of total supply
    uint256 public proposalDuration = 7 days; // duration of a proposal

    event ProposalCreated(uint256 proposalIndex, string description);
    event VoteCasted(
        uint256 proposalIndex,
        address voter,
        bool vote,
        uint256 weight
    );

    constructor(address tokenAddress) {
        token = GMchad(tokenAddress);

        proposalRequirement = token.totalSupply() / 200;
    }

    function createProposal(
        string memory title,
        string memory description
    ) public {
        require(
            bytes(title).length <= 200,
            "Title should be no longer than 200 characters."
        );
        require(
            bytes(description).length <= 500,
            "Description should be no longer than 500 characters."
        );
        require(
            token.balanceOf(msg.sender) >= proposalRequirement,
            "You must hold 0.5% of total supply to make proposals."
        );
        require(
            !hasLiveProposal[msg.sender],
            "You can only have one live proposal at a time."
        );

        proposals.push(
            Proposal({
                title: title,
                description: description,
                yesVotes: 0,
                proposer: msg.sender,
                endTimestamp: block.timestamp + proposalDuration
            })
        );
        hasLiveProposal[msg.sender] = true;

        emit ProposalCreated(proposals.length - 1, description);
    }

    function vote(uint256 proposalIndex, bool voteYes) public {
        uint256 weight = token.balanceOf(msg.sender);
        require(weight > 1, "You must have tokens to vote.");
        require(
            !hasVotedOnProposal[proposalIndex][msg.sender],
            "You have already voted on this proposal."
        );
        require(
            proposals[proposalIndex].endTimestamp > block.timestamp,
            "Voting period for this proposal has ended."
        );
        require(
            !hasVotedOnProposal[proposalIndex][msg.sender],
            "You have already voted on this proposal."
        );
        hasVotedOnProposal[proposalIndex][msg.sender] = true;

        if (voteYes) {
            proposals[proposalIndex].yesVotes += weight;
        }

        emit VoteCasted(proposalIndex, msg.sender, voteYes, weight);
    }

    function isProposalPassed(
        uint256 proposalIndex
    ) public view returns (bool) {
        return proposals[proposalIndex].yesVotes * 2 > token.totalSupply();
    }

    function getProposalsCount() public view returns (uint256) {
        return proposals.length;
    }

    function endProposal(uint256 proposalIndex) public {
        require(
            proposals[proposalIndex].endTimestamp <= block.timestamp,
            "Proposal duration has not yet ended."
        );
        hasLiveProposal[proposals[proposalIndex].proposer] = false;
    }
}