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

import {Script, console} from "forge-std/Script.sol";
import {QuotePushTreasuryManager} from "../../contracts/flash/QuotePushTreasuryManager.sol";

/**
 * @title ManageQuotePushTreasuryManager
 * @notice Harvest receiver surplus into the treasury manager and/or distribute
 *         quote to the configured gas and recycle recipients.
 *
 * Env:
 *   PRIVATE_KEY                               required
 *   QUOTE_PUSH_TREASURY_MANAGER_MAINNET       required
 *   QUOTE_PUSH_TREASURY_HARVEST               optional; default 1
 *   QUOTE_PUSH_TREASURY_GAS_DISTRIBUTION_RAW  optional; default 0
 *   QUOTE_PUSH_TREASURY_RECYCLE_DISTRIBUTION_RAW optional; default 0
 *   QUOTE_PUSH_TREASURY_TRANSFER_RECEIVER_OWNER_TO optional; when set, transfer receiver ownership
 */
contract ManageQuotePushTreasuryManager is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address managerAddr = vm.envAddress("QUOTE_PUSH_TREASURY_MANAGER_MAINNET");
        bool harvest = vm.envOr("QUOTE_PUSH_TREASURY_HARVEST", uint256(1)) == 1;
        uint256 gasAmount = vm.envOr("QUOTE_PUSH_TREASURY_GAS_DISTRIBUTION_RAW", uint256(0));
        uint256 recycleAmount = vm.envOr("QUOTE_PUSH_TREASURY_RECYCLE_DISTRIBUTION_RAW", uint256(0));
        address transferReceiverOwnerTo = vm.envOr("QUOTE_PUSH_TREASURY_TRANSFER_RECEIVER_OWNER_TO", address(0));

        QuotePushTreasuryManager manager = QuotePushTreasuryManager(managerAddr);

        console.log("manager", managerAddr);
        console.log("harvest", harvest);
        console.log("gasAmount", gasAmount);
        console.log("recycleAmount", recycleAmount);
        console.log("transferReceiverOwnerTo", transferReceiverOwnerTo);
        console.log("quoteBalanceBefore", manager.quoteBalance());
        console.log("availableBefore", manager.availableQuote());
        console.log("receiverSweepableBefore", manager.receiverSweepableQuote());

        vm.startBroadcast(pk);
        if (harvest) {
            uint256 harvested = manager.harvestReceiverSurplus();
            console.log("harvested", harvested);
        }
        if (gasAmount > 0 || recycleAmount > 0) {
            manager.distributeToConfiguredRecipients(gasAmount, recycleAmount);
        }
        if (transferReceiverOwnerTo != address(0)) {
            manager.transferManagedReceiverOwnership(transferReceiverOwnerTo);
        }
        vm.stopBroadcast();

        console.log("quoteBalanceAfter", manager.quoteBalance());
        console.log("availableAfter", manager.availableQuote());
    }
}
