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

import "./KeyVault.sol";

/// @title VKPFactory — Deploy KeyVaults with one call
/// @author Vaultfire Protocol
/// @notice Factory for creating KeyVault instances. Tracks all vaults created
///         per agent for discoverability.
contract VKPFactory {
    // ═══════════════════════════════════════════════════
    // STATE
    // ═══════════════════════════════════════════════════

    address public immutable identityRegistry;
    uint256 public vaultCount;

    /// @notice Agent address → their KeyVault addresses
    mapping(address => address[]) public agentVaults;

    /// @notice All vault addresses created through this factory
    address[] public allVaults;

    // ═══════════════════════════════════════════════════
    // EVENTS
    // ═══════════════════════════════════════════════════

    event VaultDeployed(
        address indexed owner,
        address indexed vault,
        uint256 maxDaily,
        uint256 maxPerTx
    );

    // ═══════════════════════════════════════════════════
    // ERRORS
    // ═══════════════════════════════════════════════════

    error ZeroAddress();

    // ═══════════════════════════════════════════════════
    // CONSTRUCTOR
    // ═══════════════════════════════════════════════════

    constructor(address _identityRegistry) {
        if (_identityRegistry == address(0)) revert ZeroAddress();
        identityRegistry = _identityRegistry;
    }

    // ═══════════════════════════════════════════════════
    // CREATE VAULT
    // ═══════════════════════════════════════════════════

    /// @notice Deploy a new KeyVault for the calling agent
    /// @param maxPerTx Max native token per transaction (wei)
    /// @param maxHourly Max native token per hour (wei)
    /// @param maxDaily Max native token per day (wei)
    /// @param emergencyThreshold Freeze if total spent exceeds this (wei)
    /// @return vault The address of the newly deployed KeyVault
    function createVault(
        uint256 maxPerTx,
        uint256 maxHourly,
        uint256 maxDaily,
        uint256 emergencyThreshold
    ) external returns (address vault) {
        // Pass msg.sender as owner — KeyVault constructor verifies ERC-8004 registration
        KeyVault v = new KeyVault(
            msg.sender,
            identityRegistry,
            maxPerTx,
            maxHourly,
            maxDaily,
            emergencyThreshold
        );

        vault = address(v);
        agentVaults[msg.sender].push(vault);
        allVaults.push(vault);
        vaultCount++;

        emit VaultDeployed(msg.sender, vault, maxDaily, maxPerTx);
    }

    // ═══════════════════════════════════════════════════
    // VIEW FUNCTIONS
    // ═══════════════════════════════════════════════════

    /// @notice Get all vaults for an agent
    function getAgentVaults(address agent) external view returns (address[] memory) {
        return agentVaults[agent];
    }

    /// @notice Get total number of vaults created
    function totalVaults() external view returns (uint256) {
        return vaultCount;
    }
}
