"""
AI Analysis Model für Infomaniak KI-Integration
"""
from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from app.models.base import Base, TimestampMixin


class AIAnalysis(Base, TimestampMixin):
    """
    AI Analysis-Tabelle für gecachte LLM-Antworten

    Speichert Ergebnisse von Infomaniak AI API Aufrufen zu CVEs
    """
    __tablename__ = "ai_analyses"

    id = Column(Integer, primary_key=True, index=True)
    vulnerability_id = Column(
        Integer,
        ForeignKey("vulnerabilities.id"),
        unique=True,
        nullable=False,
        index=True
    )

    # Analyse-Ergebnis
    analysis_text = Column(Text, nullable=False)

    # Strukturierte Daten (als JSON gespeichert)
    threat_level = Column(String(20), nullable=True)  # low, medium, high, critical
    exploits_found = Column(Text, nullable=True)  # JSON-Array
    workarounds = Column(Text, nullable=True)  # JSON-Array
    remediation_steps = Column(Text, nullable=True)  # JSON-Array
    threat_intel_sources = Column(Text, nullable=True)  # JSON-Array

    # Metadata
    model_version = Column(String(50), nullable=True)
    confidence_score = Column(String(20), nullable=True)  # low, medium, high
    analysis_timestamp = Column(DateTime, nullable=False)

    # Cache-Management
    cache_expires_at = Column(DateTime, nullable=False)  # TTL für Refresh

    # Relationships
    vulnerability = relationship("Vulnerability", back_populates="ai_analysis")

    def __repr__(self):
        return f"<AIAnalysis(vuln_id={self.vulnerability_id}, threat='{self.threat_level}')>"
