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

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

/**
 * @title DeployQuotePushTreasuryManager
 * @notice Deploy a treasury manager for quote-push retained surplus and optionally
 *         hand receiver ownership to it in the same broadcast sequence.
 *
 * Env:
 *   PRIVATE_KEY                                 required
 *   AAVE_QUOTE_PUSH_RECEIVER_MAINNET            required
 *   QUOTE_PUSH_SURPLUS_TOKEN_MAINNET            optional; defaults to USDC mainnet
 *   QUOTE_PUSH_TREASURY_OWNER                   optional; defaults to deployer
 *   QUOTE_PUSH_TREASURY_OPERATOR                optional; defaults to owner
 *   QUOTE_PUSH_TREASURY_GAS_RECIPIENT           optional; defaults to owner
 *   QUOTE_PUSH_TREASURY_RECYCLE_RECIPIENT       optional; defaults to owner
 *   QUOTE_PUSH_RECEIVER_RESERVE_RAW             optional; defaults to 0
 *   QUOTE_PUSH_TREASURY_RESERVE_RAW             optional; defaults to 0
 *   QUOTE_PUSH_TREASURY_TAKE_RECEIVER_OWNERSHIP optional; 1 to transfer receiver ownership to manager
 */
contract DeployQuotePushTreasuryManager is Script {
    address internal constant DEFAULT_USDC_MAINNET = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address receiver = vm.envAddress("AAVE_QUOTE_PUSH_RECEIVER_MAINNET");
        address quoteToken = vm.envOr("QUOTE_PUSH_SURPLUS_TOKEN_MAINNET", DEFAULT_USDC_MAINNET);
        address deployer = vm.addr(pk);
        address owner = vm.envOr("QUOTE_PUSH_TREASURY_OWNER", deployer);
        address operator = vm.envOr("QUOTE_PUSH_TREASURY_OPERATOR", owner);
        address gasRecipient = vm.envOr("QUOTE_PUSH_TREASURY_GAS_RECIPIENT", owner);
        address recycleRecipient = vm.envOr("QUOTE_PUSH_TREASURY_RECYCLE_RECIPIENT", owner);
        uint256 receiverReserve = vm.envOr("QUOTE_PUSH_RECEIVER_RESERVE_RAW", uint256(0));
        uint256 managerReserve = vm.envOr("QUOTE_PUSH_TREASURY_RESERVE_RAW", uint256(0));
        bool takeReceiverOwnership = vm.envOr("QUOTE_PUSH_TREASURY_TAKE_RECEIVER_OWNERSHIP", uint256(0)) == 1;

        console.log("Deployer:", deployer);
        console.log("Receiver:", receiver);
        console.log("Quote token:", quoteToken);
        console.log("Manager owner:", owner);
        console.log("Manager operator:", operator);
        console.log("Gas recipient:", gasRecipient);
        console.log("Recycle recipient:", recycleRecipient);
        console.log("Receiver reserve:", receiverReserve);
        console.log("Manager reserve:", managerReserve);
        console.log("Take receiver ownership:", takeReceiverOwnership);

        vm.startBroadcast(pk);
        QuotePushTreasuryManager manager = new QuotePushTreasuryManager(
            owner,
            receiver,
            quoteToken,
            operator,
            gasRecipient,
            recycleRecipient,
            receiverReserve,
            managerReserve
        );

        if (takeReceiverOwnership) {
            AaveQuotePushFlashReceiver(receiver).transferOwnership(address(manager));
            require(manager.isReceiverOwnedByManager(), "receiver ownership not finalized");
            console.log("Receiver ownership transferred to manager");
        }
        vm.stopBroadcast();

        console.log("QuotePushTreasuryManager:", address(manager));
    }
}
