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

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

/**
 * @title DeployDODOPMMIntegration
 * @notice Deploy DODO PMM Integration contract for liquidity pools
 * @dev This contract should be deployed on the same chain as compliant tokens (Chain 138)
 *      and on chains where official USDT/USDC exist for pool creation
 */
contract DeployDODOPMMIntegration is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);

        // Load environment variables (required for deploy)
        address admin = vm.envOr("DODO_INTEGRATION_ADMIN", deployer);
        address dodoVendingMachine = vm.envOr("DODO_VENDING_MACHINE_ADDRESS", address(0));
        address dodoApprove = vm.envOr("DODO_APPROVE_ADDRESS", address(0)); // Optional
        address officialUSDT = vm.envOr("OFFICIAL_USDT_ADDRESS", address(0));
        address officialUSDC = vm.envOr("OFFICIAL_USDC_ADDRESS", address(0));
        address compliantUSDT = vm.envOr("COMPLIANT_USDT_ADDRESS", address(0));
        address compliantUSDC = vm.envOr("COMPLIANT_USDC_ADDRESS", address(0));

        if (
            dodoVendingMachine == address(0) ||
            officialUSDT == address(0) ||
            officialUSDC == address(0) ||
            compliantUSDT == address(0) ||
            compliantUSDC == address(0)
        ) {
            console.log(
                "Skipping DODO PMM deploy: set DODO_VENDING_MACHINE_ADDRESS, OFFICIAL_USDT_ADDRESS, OFFICIAL_USDC_ADDRESS, COMPLIANT_USDT_ADDRESS, COMPLIANT_USDC_ADDRESS in .env"
            );
            return;
        }

        console.log("Deploying DODOPMMIntegration with deployer:", vm.toString(deployer));
        console.log("Admin:", vm.toString(admin));
        console.log("DODO Vending Machine:", vm.toString(dodoVendingMachine));
        console.log("DODO Approve:", vm.toString(dodoApprove));
        console.log("Official USDT:", vm.toString(officialUSDT));
        console.log("Official USDC:", vm.toString(officialUSDC));
        console.log("Compliant USDT:", vm.toString(compliantUSDT));
        console.log("Compliant USDC:", vm.toString(compliantUSDC));

        vm.startBroadcast(deployerPrivateKey);
        
        DODOPMMIntegration integration = new DODOPMMIntegration(
            admin,
            dodoVendingMachine,
            dodoApprove,
            officialUSDT,
            officialUSDC,
            compliantUSDT,
            compliantUSDC
        );
        
        console.log("DODOPMMIntegration deployed at:", vm.toString(address(integration)));
        
        vm.stopBroadcast();
        
        console.log("\n=== Deployment Summary ===");
        console.log("DODOPMMIntegration:", vm.toString(address(integration)));
        console.log("Admin:", vm.toString(admin));
        console.log("DODO Vending Machine:", vm.toString(dodoVendingMachine));
        console.log("\nNext Steps:");
        console.log("1. Create pools using createCUSDTUSDTPool() and createCUSDCUSDCPool()");
        console.log("2. Add initial liquidity to pools");
        console.log("3. Configure pool parameters (lpFeeRate, k, initialPrice)");
    }
}
