// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.19;

import { IWETH9 } from "../src/interfaces/IWETH9.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";

/**
 * @title SeedSwapper
 * @notice One-shot helper deployed during Deploy*.run() to perform the initial seed buy.
 *         Executing a small buy before the protocol opens eliminates the cumulativeVolume==0
 *         front-run window: after the seed recenter, VWAP has a real anchor and the bootstrap
 *         path in LiquidityManager.recenter() is never reachable by external users.
 *
 * @dev Deployed by both DeployLocal and DeployBase; extracted here to avoid duplicate
 *      Foundry artifacts and drift between the two deploy scripts.
 */
contract SeedSwapper {
    // TickMath sentinel values — the pool will consume as much input as available in-range.
    uint160 private constant SQRT_PRICE_LIMIT_MIN = 4295128740; // TickMath.MIN_SQRT_RATIO + 1
    uint160 private constant SQRT_PRICE_LIMIT_MAX = // TickMath.MAX_SQRT_RATIO - 1
        1461446703485210103287273052203988822378723970341;

    IWETH9 private immutable weth;
    IUniswapV3Pool private immutable pool;
    bool private immutable token0isWeth;

    constructor(address _weth, address _pool, bool _token0isWeth) {
        weth = IWETH9(_weth);
        pool = IUniswapV3Pool(_pool);
        token0isWeth = _token0isWeth;
    }

    /// @notice Wraps msg.value ETH to WETH, swaps it for KRK (buying KRK), and sweeps any
    ///         unconsumed WETH back to `recipient`.  The fee generated by the swap is captured
    ///         in the LM's anchor position so the subsequent recenter() call will collect a
    ///         non-zero ethFee and record VWAP.
    ///
    /// @dev    If the pool exhausts in-range liquidity before spending all WETH (price-limit
    ///         sentinel reached), `wethDelta < msg.value` in the callback and the remainder
    ///         stays as WETH in this contract.  The post-swap sweep returns it to `recipient`
    ///         so no funds are stranded.
    function executeSeedBuy(address recipient) external payable {
        weth.deposit{ value: msg.value }();

        // zeroForOne=true  when WETH is token0: sell token0(WETH) -> token1(KRK)
        // zeroForOne=false when WETH is token1: sell token1(WETH) -> token0(KRK)
        bool zeroForOne = token0isWeth;
        uint160 priceLimit = zeroForOne ? SQRT_PRICE_LIMIT_MIN : SQRT_PRICE_LIMIT_MAX;

        pool.swap(recipient, zeroForOne, int256(msg.value), priceLimit, "");

        // Sweep any unconsumed WETH to recipient (occurs when price limit is reached
        // before the full input is spent).
        uint256 leftover = weth.balanceOf(address(this));
        if (leftover > 0) {
            require(weth.transfer(recipient, leftover), "SeedSwapper: WETH sweep failed");
        }
    }

    /// @notice Uniswap V3 callback: pay the WETH owed for the seed buy.
    function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) external {
        require(msg.sender == address(pool), "SeedSwapper: only pool");
        int256 wethDelta = token0isWeth ? amount0Delta : amount1Delta;
        if (wethDelta > 0) {
            require(weth.transfer(msg.sender, uint256(wethDelta)), "SeedSwapper: WETH transfer failed");
        }
    }
}
