"""VulnerabilitySnapshot DTO"""

from dataclasses import dataclass, field
from typing import Optional, Dict, Any
from decimal import Decimal


@dataclass
class VulnerabilitySnapshotDTO:
    """漏洞快照 DTO

    对应 VulnerabilitySnapshot 模型，用于在 Service/Task 之间传递漏洞结果数据。

    设计与其他快照 DTO 一致：
    - scan_id: 只属于快照表
    - target_id: 只用于转换为资产 DTO，不直接存入快照表
    """

    scan_id: int
    target_id: int  # 仅用于传递数据和生成资产 DTO，不保存到快照表
    url: str
    vuln_type: str
    severity: str
    source: str = ""
    cvss_score: Optional[Decimal] = None
    description: str = ""
    raw_output: Dict[str, Any] = field(default_factory=dict)

    def to_asset_dto(self):
        """转换为漏洞资产 DTO（用于同步到 Vulnerability 表）。"""
        from apps.asset.dtos.asset import VulnerabilityDTO

        return VulnerabilityDTO(
            target_id=self.target_id,
            url=self.url,
            vuln_type=self.vuln_type,
            severity=self.severity,
            source=self.source,
            cvss_score=self.cvss_score,
            description=self.description,
            raw_output=self.raw_output,
        )
