"""Vector generation tool — dynamic attack vector creation for the pentesting agent.

Provides ``generate_vectors`` that invokes the DynamicVectorGenerator
to create capability-tailored attack vectors on demand.
"""

from __future__ import annotations

import logging
from typing import Any

logger = logging.getLogger(__name__)


def generate_vectors(
    *,
    capabilities: list[str],
    focus_categories: list[str] | None = None,
    count: int = 5,
) -> dict[str, Any]:
    """Generate dynamic attack vectors tailored to discovered capabilities.

    Uses the rule-based DynamicVectorGenerator to create attack vectors
    that target specific agent capabilities and weaknesses.

    Args:
        capabilities: List of discovered capability names/descriptions.
        focus_categories: Attack categories to focus on (e.g. 'prompt_injection').
        count: Maximum number of vectors to return.

    Returns:
        Generated vectors with prompts and metadata.
    """
    from ziran.application.dynamic_vectors.generator import DynamicVectorGenerator
    from ziran.domain.entities.capability import AgentCapability, CapabilityType

    generator = DynamicVectorGenerator()

    # Convert string capability names to AgentCapability objects
    cap_objects = [
        AgentCapability(
            id=cap_name,
            name=cap_name,
            type=CapabilityType.TOOL,
            description=f"Capability: {cap_name}",
        )
        for cap_name in capabilities
    ]

    try:
        vectors = generator.generate(cap_objects)

        result_vectors = []
        for v in vectors:
            if focus_categories and v.category.value not in focus_categories:
                continue
            vec_dict: dict[str, Any] = {
                "id": v.id,
                "name": v.name,
                "category": v.category.value,
                "severity": v.severity,
                "description": v.description,
                "target_phase": v.target_phase.value,
                "prompt_count": v.prompt_count,
            }
            result_vectors.append(vec_dict)

        return {
            "generated_count": len(result_vectors[:count]),
            "vectors": result_vectors[:count],
            "capabilities_used": capabilities,
        }
    except Exception as e:
        logger.warning("Dynamic vector generation failed: %s", e)
        return {
            "generated_count": 0,
            "vectors": [],
            "error": str(e),
        }
