// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

contract Hen_House_Cock_Splitter_V2 {
    address public owner;

    // Initialize ratios and wallet addresses as  state variables
    uint256 public xbcCoopRatio;
    uint256 public bakaawRatio;
    uint256 public futureBirdsRatio;
    uint256 public gasWarsRatio;

    address payable public xbcCoopAddress;
    address payable public bakaawAddress;
    address payable public futureBirdsAddress;
    address payable public gasWarsAddress;

    uint256 public constant MASTER_RATIO_DIVISOR = 1000;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);


    modifier onlyOwner() {
        require(owner == msg.sender, "Caller is not the owner.");
        _;
    }

    constructor() {
        owner = msg.sender;
        // Set initial hardcoded values
        xbcCoopRatio = 250; // Ratio for XBC Coop
        bakaawRatio = 250; // Ratio for Bakaaw
        futureBirdsRatio = 250; // Ratio for Future Birds
        gasWarsRatio = 250;  // Ratio for GasWars

        xbcCoopAddress = payable(0x68ab57FfeD898c059c1cBbcB409EC53C350fF715); // Now Hen House Hustle, Cock Coop has been upgraded
        bakaawAddress = payable(0xE01c9dE5751b9f8EcE6F2D1B28B6444f0b0A3D14); // Bakaaw Address
        futureBirdsAddress = payable(0xE01c9dE5751b9f8EcE6F2D1B28B6444f0b0A3D14); // Future Birds Address
        gasWarsAddress = payable(0xE01c9dE5751b9f8EcE6F2D1B28B6444f0b0A3D14); // Future Gas Wars Address
    }

    receive() external payable {}

    // Update the fee ratios, XBC COOP will always have atleast 25%
    function updateRatios( uint256 newBakaawRatio, uint256 newFutureBirdsRatio, uint256 newGasWarRatio) public onlyOwner {
        uint256 totalRatio = xbcCoopRatio + newBakaawRatio + newFutureBirdsRatio + gasWarsRatio;
        require(totalRatio <= MASTER_RATIO_DIVISOR, "Total ratio must be 1000.");
        
        bakaawRatio = newBakaawRatio;
        futureBirdsRatio = newFutureBirdsRatio;
        gasWarsRatio = newGasWarRatio;
    }

    // Update the wallet addresses
    function updateWalletAddresses(address payable newBakaaw, address payable newFutureBirds, address payable newGasWars) public onlyOwner {
        bakaawAddress = newBakaaw;
        futureBirdsAddress = newFutureBirds;
        gasWarsAddress = newGasWars;

    }

    // Split ETH between XBC Coop, Bakaaw, and Future birds
    function splitETH() public {
        uint256 balance = address(this).balance;

        uint256 xbcCoopShare = (balance * xbcCoopRatio) / MASTER_RATIO_DIVISOR;
        uint256 bakaawShare = (balance * bakaawRatio) / MASTER_RATIO_DIVISOR;
        uint256 gasWarShare = (balance * gasWarsRatio) / MASTER_RATIO_DIVISOR;
        uint256 futureBirdsShare = balance - xbcCoopShare - bakaawShare - gasWarShare; // Remainder to avoid rounding errors

        _safeTransfer(xbcCoopAddress, xbcCoopShare);
        _safeTransfer(bakaawAddress, bakaawShare);
        _safeTransfer(futureBirdsAddress, futureBirdsShare);
        _safeTransfer(gasWarsAddress, gasWarShare);
    }

    function _safeTransfer(address payable recipient, uint256 amount) private {
        (bool success, ) = recipient.call{value: amount, gas: 40000}("");
        require(success, "Transfer failed");
    }

    function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0), "New owner is the zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

}