"""
Scan Model für Scan-Historie
"""
from enum import Enum
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum as SQLEnum, Text
from sqlalchemy.orm import relationship
from app.models.base import Base, TimestampMixin


class ScanType(str, Enum):
    """Typ des Scans"""
    FULL = "full"               # legacy — older scheduled Wazuh runs
    SYSCOLLECTOR = "syscollector"  # legacy — older autoscan Wazuh runs
    NESSUS = "nessus"
    WAZUH = "wazuh"             # canonical Wazuh source label


class ScanStatus(str, Enum):
    """Status des Scans"""
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"


class Scan(Base, TimestampMixin):
    """
    Scan-Tabelle für Tracking von Vulnerability-Scans

    Dokumentiert wann und wie Assets gescannt wurden
    """
    __tablename__ = "scans"

    id = Column(Integer, primary_key=True, index=True)
    asset_id = Column(Integer, ForeignKey("assets.id"), nullable=False, index=True)

    # Scan-Details
    scan_type = Column(
        SQLEnum(ScanType),
        default=ScanType.FULL,
        nullable=False
    )
    status = Column(
        SQLEnum(ScanStatus),
        default=ScanStatus.PENDING,
        nullable=False,
        index=True
    )

    # Zeitstempel
    started_at = Column(DateTime, nullable=True)
    completed_at = Column(DateTime, nullable=True)

    # Ergebnisse
    vulnerabilities_found = Column(Integer, default=0, nullable=False)
    vulnerabilities_new = Column(Integer, default=0, nullable=False)
    vulnerabilities_resolved = Column(Integer, default=0, nullable=False)

    # Fehlerbehandlung
    error_message = Column(Text, nullable=True)

    # Relationships
    asset = relationship("Asset", back_populates="scans")

    def __repr__(self):
        return f"<Scan(asset_id={self.asset_id}, status='{self.status}')>"
