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

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

contract DeployMultiSig is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        // Get owners from environment or use deployer as single owner
        address[] memory owners = new address[](1);
        owners[0] = vm.envOr("MULTISIG_OWNER_1", deployer);
        
        // If additional owners provided, add them
        address owner2 = vm.envOr("MULTISIG_OWNER_2", address(0));
        address owner3 = vm.envOr("MULTISIG_OWNER_3", address(0));
        
        if (owner2 != address(0)) {
            address[] memory owners2 = new address[](2);
            owners2[0] = owners[0];
            owners2[1] = owner2;
            owners = owners2;
        }
        
        if (owner3 != address(0)) {
            address[] memory owners3 = new address[](3);
            owners3[0] = owners[0];
            owners3[1] = owners.length > 1 ? owners[1] : address(0);
            owners3[2] = owner3;
            owners = owners3;
        }
        
        uint256 required = vm.envOr("MULTISIG_REQUIRED", uint256(1));
        require(required > 0 && required <= owners.length, "Invalid required confirmations");
        
        vm.startBroadcast(deployerPrivateKey);
        
        MultiSig multisig = new MultiSig(owners, required);
        
        console.log("MultiSig deployed at:", address(multisig));
        console.log("Required confirmations:", required);
        console.log("Owners count:", owners.length);
        
        vm.stopBroadcast();
    }
}

