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

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

/**
 * @title DeployPrivatePoolRegistryAndPools
 * @notice Deploy PrivatePoolRegistry and optionally create XAU-anchored private pools (Master Plan Phase 2).
 * @dev Env: PRIVATE_KEY, (optional) PRIVATE_POOL_REGISTRY_ADMIN, DODOPMM_INTEGRATION_ADDRESS,
 *      XAU_ADDRESS_138, COMPLIANT_USDT_ADDRESS, COMPLIANT_USDC_ADDRESS, cEURT_ADDRESS_138.
 *      Deployer must have POOL_MANAGER_ROLE on DODOPMMIntegration to create pools.
 */
contract DeployPrivatePoolRegistryAndPools is Script {
    uint256 constant LP_FEE_BPS = 3;
    uint256 constant INITIAL_PRICE_1E18 = 1e18;
    uint256 constant K_50PCT = 0.5e18;
    bool constant USE_TWAP = true;

    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        address admin = vm.envOr("PRIVATE_POOL_REGISTRY_ADMIN", deployer);

        vm.startBroadcast(deployerPrivateKey);

        PrivatePoolRegistry registry = new PrivatePoolRegistry(admin);
        console.log("PrivatePoolRegistry deployed at:", vm.toString(address(registry)));

        address integrationAddr = vm.envOr("DODOPMM_INTEGRATION_ADDRESS", address(0));
        address xau = vm.envOr("XAU_ADDRESS_138", address(0));

        if (integrationAddr != address(0) && xau != address(0)) {
            address cUSDT = vm.envOr("COMPLIANT_USDT_ADDRESS", address(0));
            address cUSDC = vm.envOr("COMPLIANT_USDC_ADDRESS", address(0));
            address cEURT = vm.envOr("cEURT_ADDRESS_138", address(0));

            DODOPMMIntegration integration = DODOPMMIntegration(integrationAddr);

            if (cUSDT != address(0)) {
                try integration.createPool(cUSDT, xau, LP_FEE_BPS, INITIAL_PRICE_1E18, K_50PCT, USE_TWAP) returns (address pool) {
                    registry.register(cUSDT, xau, pool);
                    console.log("Created and registered cUSDT/XAU pool:", vm.toString(pool));
                } catch {}
            }
            if (cUSDC != address(0)) {
                try integration.createPool(cUSDC, xau, LP_FEE_BPS, INITIAL_PRICE_1E18, K_50PCT, USE_TWAP) returns (address pool) {
                    registry.register(cUSDC, xau, pool);
                    console.log("Created and registered cUSDC/XAU pool:", vm.toString(pool));
                } catch {}
            }
            if (cEURT != address(0)) {
                try integration.createPool(cEURT, xau, LP_FEE_BPS, INITIAL_PRICE_1E18, K_50PCT, USE_TWAP) returns (address pool) {
                    registry.register(cEURT, xau, pool);
                    console.log("Created and registered cEURT/XAU pool:", vm.toString(pool));
                } catch {}
            }
        } else {
            console.log("Skipping pool creation (set DODOPMM_INTEGRATION_ADDRESS and XAU_ADDRESS_138 to create XAU-anchored pools)");
        }

        vm.stopBroadcast();

        console.log("\n=== Deployment Summary ===");
        console.log("PrivatePoolRegistry:", vm.toString(address(registry)));
        console.log("Admin:", vm.toString(admin));
    }
}
