"""Vulnerability data model - represents findings from live model testing"""

from dataclasses import dataclass, field
from typing import Any, Dict, List

from .finding import Severity


@dataclass
class LiveVulnerability:
    """
    Represents a vulnerability found during live model testing.

    Different from static Finding - includes prompt/response data
    and is focused on runtime behavior rather than code patterns.
    """

    # Required fields
    id: str
    detector_id: str
    severity: Severity
    confidence: float  # 0.0-1.0
    title: str
    description: str

    # Test data
    prompt_used: str
    response_received: str

    # Evidence for confidence calculation
    evidence: Dict[str, Any] = field(default_factory=dict)

    # Remediation guidance
    remediation: str = ""

    # Additional metadata
    metadata: Dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        """Convert to JSON-serializable dict"""
        return {
            "id": self.id,
            "detector_id": self.detector_id,
            "severity": self.severity.value,
            "confidence": round(self.confidence, 2),
            "title": self.title,
            "description": self.description,
            "prompt_used": self.prompt_used,
            "response_received": self.response_received,
            "evidence": self.evidence,
            "remediation": self.remediation,
            "metadata": self.metadata,
        }

    def __str__(self) -> str:
        """Human-readable representation"""
        return (
            f"[{self.severity.value}] {self.title} "
            f"(confidence: {self.confidence:.0%}, detector: {self.detector_id})"
        )


@dataclass
class LiveTestResult:
    """Result from a single live detector run."""

    detector_id: str
    detector_name: str
    score: float  # 0-100 (higher = more secure)
    confidence: float  # 0.0-1.0
    vulnerabilities: List[LiveVulnerability] = field(default_factory=list)
    tests_run: int = 0
    tests_passed: int = 0
    duration_ms: float = 0.0
    metadata: Dict[str, Any] = field(default_factory=dict)

    @property
    def tests_failed(self) -> int:
        """Number of failed tests"""
        return self.tests_run - self.tests_passed

    @property
    def pass_rate(self) -> float:
        """Pass rate as percentage"""
        if self.tests_run == 0:
            return 100.0
        return (self.tests_passed / self.tests_run) * 100

    def to_dict(self) -> Dict[str, Any]:
        """Convert to JSON-serializable dict"""
        return {
            "detector_id": self.detector_id,
            "detector_name": self.detector_name,
            "score": round(self.score, 2),
            "confidence": round(self.confidence, 2),
            "vulnerabilities": [v.to_dict() for v in self.vulnerabilities],
            "tests_run": self.tests_run,
            "tests_passed": self.tests_passed,
            "tests_failed": self.tests_failed,
            "pass_rate": round(self.pass_rate, 2),
            "duration_ms": round(self.duration_ms, 2),
            "metadata": self.metadata,
        }
