"""
Data models for Vulnerability Checker.
"""

import datetime
import enum
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any


class CheckStatus(enum.Enum):
    """Check status."""
    PASSED = "PASSED"
    FAILED = "FAILED"
    ERROR = "ERROR"
    SKIPPED = "SKIPPED"


class Severity(enum.Enum):
    """Vulnerability severity level."""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    CRITICAL = "critical"


@dataclass
class CheckResult:
    """Check result."""
    status: CheckStatus
    issues_count: int = 0
    message: Optional[str] = None
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            "status": self.status.value,
            "issues_count": self.issues_count,
            "message": self.message,
        }


@dataclass
class VulnerabilityIssue:
    """Detected vulnerability."""
    issue_type: str
    severity: str
    description: str
    file_path: str
    line_number: int
    code_snippet: str = ""
    confidence: str = "MEDIUM"
    cwe_id: Optional[str] = None
    owasp_category: Optional[str] = None
    remediation: Optional[str] = None
    check_name: Optional[str] = None
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            "issue_type": self.issue_type,
            "severity": self.severity,
            "description": self.description,
            "file_path": self.file_path,
            "line_number": self.line_number,
            "code_snippet": self.code_snippet,
            "confidence": self.confidence,
            "cwe_id": self.cwe_id,
            "owasp_category": self.owasp_category,
            "remediation": self.remediation,
            "check_name": self.check_name,
        }


@dataclass
class VulnerabilityReport:
    """Vulnerability scan report."""
    target_path: str
    scan_date: str = field(default_factory=lambda: datetime.datetime.now().isoformat())
    issues: List[VulnerabilityIssue] = field(default_factory=list)
    checks_status: Dict[str, CheckResult] = field(default_factory=dict)
    total_issues: int = 0
    scan_duration_seconds: float = 0.0
    scanner_version: str = "0.1.0"
    metadata: Dict[str, Any] = field(default_factory=dict)
    
    def add_issue(self, issue: VulnerabilityIssue) -> None:
        """Add vulnerability to the report."""
        self.issues.append(issue)
        self.total_issues += 1
    
    def add_check_result(self, check_name: str, result: CheckResult) -> None:
        """Add check result to the report."""
        self.checks_status[check_name] = result
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary."""
        return {
            "target_path": self.target_path,
            "scan_date": self.scan_date,
            "issues": [issue.to_dict() for issue in self.issues],
            "checks_status": {
                check: status.to_dict() 
                for check, status in self.checks_status.items()
            },
            "total_issues": self.total_issues,
            "scan_duration_seconds": self.scan_duration_seconds,
            "scanner_version": self.scanner_version,
            "metadata": self.metadata,
        } 