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

import "forge-std/Script.sol";
import "forge-std/console.sol";

/**
 * @title DeploySmartAccountsKit
 * @notice Deploys Smart Accounts Kit contracts (EntryPoint, AccountFactory, Paymaster)
 * @dev This script deploys the core ERC-4337 contracts needed for MetaMask Smart Accounts Kit
 * 
 * NOTE: MetaMask Smart Accounts Kit uses standard ERC-4337 contracts:
 * - EntryPoint: Standard ERC-4337 EntryPoint contract
 * - AccountFactory: Factory contract for creating smart accounts
 * - Paymaster: Optional contract for gas abstraction
 * 
 * These contracts may need to be:
 * 1. Deployed from MetaMask's provided contracts (if available)
 * 2. Deployed from standard ERC-4337 implementations (e.g., OpenZeppelin, Alchemy)
 * 3. Or use existing deployed addresses if available on ChainID 138
 */
contract DeploySmartAccountsKit is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        string memory rpcUrl = vm.envString("RPC_URL_138");
        
        console.log("Deployer:", deployer);
        console.log("RPC URL:", rpcUrl);
        console.log("Chain ID: 138");
        console.log("");
        
        // Use pre-deployed addresses from env if set (ENTRY_POINT, SMART_ACCOUNT_FACTORY, PAYMASTER)
        address entryPoint = vm.envOr("ENTRY_POINT", address(0));
        address accountFactory = vm.envOr("SMART_ACCOUNT_FACTORY", address(0));
        address paymaster = vm.envOr("PAYMASTER", address(0));
        
        vm.startBroadcast(deployerPrivateKey);
        
        // EntryPoint: use env or deploy (requires ERC-4337 EntryPoint implementation)
        console.log("=== EntryPoint ===");
        if (entryPoint != address(0)) {
            console.log("Using ENTRY_POINT from env:", entryPoint);
        } else {
            console.log("ENTRY_POINT not set. Deploy EntryPoint (ERC-4337) and set ENTRY_POINT in .env");
            console.log("Source: Standard ERC-4337 EntryPoint or MetaMask Smart Accounts Kit");
        }
        console.log("");
        
        // AccountFactory: use env or deploy
        console.log("=== AccountFactory ===");
        if (accountFactory != address(0)) {
            console.log("Using SMART_ACCOUNT_FACTORY from env:", accountFactory);
        } else {
            console.log("SMART_ACCOUNT_FACTORY not set. Deploy factory and set SMART_ACCOUNT_FACTORY in .env");
            console.log("Source: MetaMask Smart Accounts Kit or compatible ERC-4337 factory");
        }
        console.log("");
        
        // Paymaster: optional
        console.log("=== Paymaster (Optional) ===");
        if (paymaster != address(0)) {
            console.log("Using PAYMASTER from env:", paymaster);
        } else {
            console.log("PAYMASTER not set (optional). Deploy for gas abstraction when needed.");
        }
        console.log("");
        
        vm.stopBroadcast();
        
        console.log("=== Deployment Summary ===");
        console.log("EntryPoint:", entryPoint);
        console.log("AccountFactory:", accountFactory);
        console.log("Paymaster:", paymaster);
        console.log("");
        console.log("=== Next Steps ===");
        console.log("1. Deploy EntryPoint contract (ERC-4337 standard)");
        console.log("2. Deploy AccountFactory contract (MetaMask Smart Accounts Kit)");
        console.log("3. Deploy Paymaster contract (optional, for gas abstraction)");
        console.log("4. Update config/smart-accounts-config.json with deployed addresses");
        console.log("5. Deploy AccountWalletRegistryExtended with factory and entry point addresses");
        console.log("");
        console.log("=== Important Notes ===");
        console.log("- EntryPoint and AccountFactory contracts need to be obtained from:");
        console.log("  * MetaMask Smart Accounts Kit SDK/package");
        console.log("  * Standard ERC-4337 implementations (OpenZeppelin, Alchemy, etc.)");
        console.log("  * Or use existing deployed addresses if available");
        console.log("- Once contracts are deployed, update .env with addresses:");
        console.log("  ENTRY_POINT=<deployed-entrypoint-address>");
        console.log("  SMART_ACCOUNT_FACTORY=<deployed-factory-address>");
        console.log("  PAYMASTER=<deployed-paymaster-address> (optional)");
    }
}
