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

import "../src/Kraiken.sol";

import { LiquidityManager } from "../src/LiquidityManager.sol";
import "../src/Optimizer.sol";
import "../src/Stake.sol";
import "../src/helpers/UniswapHelpers.sol";
import { ERC1967Proxy } from "@openzeppelin/proxy/ERC1967/ERC1967Proxy.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Factory.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
import "forge-std/Script.sol";

/**
 * @title DeployLocal
 * @notice Deployment script for local Anvil fork
 * @dev Run with: forge script script/DeployLocal.sol --rpc-url http://localhost:8545 --broadcast
 */
contract DeployLocal is Script {
    using UniswapHelpers for IUniswapV3Pool;

    uint24 internal constant FEE = uint24(10_000);

    // Configuration
    // NOTE: 0xf6a3... carries 171 bytes of code on Base mainnet and has been confirmed to
    // also carry code on Base Sepolia. bootstrap.sh erases it via anvil_setCode before
    // setFeeDestination is called so the fork-local LiquidityManager does not permanently
    // lock feeDestinationLocked. See issue #760.
    address internal constant feeDest = 0xf6a3eef9088A255c32b6aD2025f83E57291D9011;
    address internal constant weth = 0x4200000000000000000000000000000000000006;
    // Base Sepolia Uniswap V3 Factory — intentional: bootstrap.sh forks Base Sepolia,
    // so the factory must carry code at this address. Mainnet scripts and AttackRunner
    // use 0x33128a8fC17869897dcE68Ed026d694621f6FDfD instead.
    address internal constant v3Factory = 0x4752ba5DBc23f44D87826276BF6Fd6b1C372aD24;

    // Deployed contracts
    Kraiken public kraiken;
    Stake public stake;
    LiquidityManager public liquidityManager;
    IUniswapV3Pool public pool;
    bool public token0isWeth;

    function run() public {
        // Use local mnemonic file for consistent deployment
        string memory seedPhrase = vm.readFile(".secret.local");
        uint256 privateKey = vm.deriveKey(seedPhrase, 0);
        vm.startBroadcast(privateKey);
        address sender = vm.addr(privateKey);

        console.log("\n=== Starting Local Deployment ===");
        console.log("Deployer:", sender);
        console.log("Using mnemonic from .secret.local");
        console.log("Chain ID: 31337 (Local Anvil)");

        // Deploy Kraiken token
        kraiken = new Kraiken("Kraiken", "KRK");
        console.log("\n[1/6] Kraiken deployed:", address(kraiken));

        // Determine token ordering
        token0isWeth = address(weth) < address(kraiken);
        console.log("      Token ordering - WETH is token0:", token0isWeth);

        // Deploy Stake contract
        stake = new Stake(address(kraiken), feeDest);
        console.log("\n[2/6] Stake deployed:", address(stake));

        // Set staking pool in Kraiken
        kraiken.setStakingPool(address(stake));
        console.log("      Staking pool set in Kraiken");

        // Get or create Uniswap V3 pool
        IUniswapV3Factory factory = IUniswapV3Factory(v3Factory);
        address liquidityPool = factory.getPool(weth, address(kraiken), FEE);
        if (liquidityPool == address(0)) {
            liquidityPool = factory.createPool(weth, address(kraiken), FEE);
            console.log("\n[3/6] Uniswap pool created:", liquidityPool);
        } else {
            console.log("\n[3/6] Using existing pool:", liquidityPool);
        }
        pool = IUniswapV3Pool(liquidityPool);

        // Initialize pool at 1 cent price if not already initialized
        try pool.slot0() returns (uint160 sqrtPriceX96, int24, uint16, uint16, uint16, uint8, bool) {
            if (sqrtPriceX96 == 0) {
                pool.initializePoolFor1Cent(token0isWeth);
                console.log("      Pool initialized at 1 cent price");
            } else {
                console.log("      Pool already initialized");
            }
        } catch {
            pool.initializePoolFor1Cent(token0isWeth);
            console.log("      Pool initialized at 1 cent price");
        }

        // Deploy Optimizer
        Optimizer optimizerImpl = new Optimizer();
        bytes memory params = abi.encodeWithSignature("initialize(address,address)", address(kraiken), address(stake));
        ERC1967Proxy proxy = new ERC1967Proxy(address(optimizerImpl), params);
        address optimizerAddress = address(proxy);
        console.log("\n[4/6] Optimizer deployed:", optimizerAddress);

        // Deploy LiquidityManager
        liquidityManager = new LiquidityManager(v3Factory, weth, address(kraiken), optimizerAddress);
        console.log("\n[5/6] LiquidityManager deployed:", address(liquidityManager));

        // Configure contracts
        kraiken.setLiquidityManager(address(liquidityManager));
        console.log("      LiquidityManager set in Kraiken");

        // Set the real feeDestination.
        liquidityManager.setFeeDestination(feeDest);

        console.log("\n[6/6] Configuration complete");
        console.log("      feeDestination set to", feeDest);
        console.log("      VWAP bootstrap will be performed by the bootstrap script");

        // Print deployment summary
        console.log("\n=== Deployment Summary ===");
        console.log("Kraiken (KRK):", address(kraiken));
        console.log("Stake:", address(stake));
        console.log("Pool:", address(pool));
        console.log("LiquidityManager:", address(liquidityManager));
        console.log("Optimizer:", optimizerAddress);

        console.log("\n=== Next Steps ===");
        console.log("1. bootstrap-common.sh bootstrap_vwap() advances chain time and seeds VWAP.");
        console.log("2. Fund LiquidityManager with operational ETH:");
        console.log("   cast send", address(liquidityManager), "--value 10ether");
        console.log("3. recenter() is permissionless - any address (e.g. txnBot) can call it.");
        console.log("   TWAP manipulation protection is always enforced (no bypass path).");

        vm.stopBroadcast();
    }
}
