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

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

/**
 * @title SweepAaveQuotePushFlashReceiverSurplus
 * @notice Owner-operated surplus recovery for a deployed AaveQuotePushFlashReceiver.
 *
 * Env:
 *   PRIVATE_KEY                        required (must be receiver owner)
 *   AAVE_QUOTE_PUSH_RECEIVER_MAINNET   required
 *   QUOTE_PUSH_SURPLUS_TOKEN_MAINNET   optional; defaults to USDC mainnet
 *   QUOTE_PUSH_SURPLUS_RECIPIENT       optional; defaults to deployer derived from PRIVATE_KEY
 *   QUOTE_PUSH_SURPLUS_RESERVE_RAW     optional; keep this much on receiver when sweeping surplus mode
 *   QUOTE_PUSH_SURPLUS_EXACT_AMOUNT_RAW optional; if > 0, sweep this exact amount via sweepToken()
 */
contract SweepAaveQuotePushFlashReceiverSurplus 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 token = vm.envOr("QUOTE_PUSH_SURPLUS_TOKEN_MAINNET", DEFAULT_USDC_MAINNET);
        address deployer = vm.addr(pk);
        address recipient = vm.envOr("QUOTE_PUSH_SURPLUS_RECIPIENT", deployer);
        uint256 reserveRetained = vm.envOr("QUOTE_PUSH_SURPLUS_RESERVE_RAW", uint256(0));
        uint256 exactAmount = vm.envOr("QUOTE_PUSH_SURPLUS_EXACT_AMOUNT_RAW", uint256(0));

        console.log("receiver", receiver);
        console.log("token", token);
        console.log("recipient", recipient);
        console.log("reserveRetained", reserveRetained);
        console.log("exactAmount", exactAmount);

        vm.startBroadcast(pk);
        if (exactAmount > 0) {
            AaveQuotePushFlashReceiver(receiver).sweepToken(token, recipient, exactAmount);
            console.log("sweptExact", exactAmount);
        } else {
            uint256 swept = AaveQuotePushFlashReceiver(receiver).sweepQuoteSurplus(token, recipient, reserveRetained);
            console.log("sweptSurplus", swept);
        }
        vm.stopBroadcast();
    }
}
