from datetime import datetime

from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship

from .database import Base


class Network(Base):
    __tablename__ = "networks"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(80), unique=True, nullable=False, index=True)
    description = Column(Text, nullable=False)
    common_threats = Column(Text, nullable=False)
    recommended_protection = Column(Text, nullable=False)


class Threat(Base):
    __tablename__ = "threats"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(120), unique=True, nullable=False, index=True)
    description = Column(Text, nullable=False)
    probability = Column(Integer, nullable=False)
    confidentiality = Column(Integer, nullable=False)
    integrity = Column(Integer, nullable=False)
    availability = Column(Integer, nullable=False)
    affected_assets = Column(Text, nullable=False)
    category = Column(String(80), nullable=False)

    recommendations = relationship("Recommendation", back_populates="threat")


class Technology(Base):
    __tablename__ = "technologies"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(120), unique=True, nullable=False, index=True)
    description = Column(Text, nullable=False)
    effectiveness = Column(Float, nullable=False)
    protection_type = Column(String(120), nullable=False)
    target_threats = Column(Text, nullable=False)
    advantages = Column(Text, nullable=False)
    limitations = Column(Text, nullable=False)
    suitable_networks = Column(Text, nullable=False)

    recommendations = relationship("Recommendation", back_populates="technology")


class Recommendation(Base):
    __tablename__ = "recommendations"

    id = Column(Integer, primary_key=True, index=True)
    threat_id = Column(Integer, ForeignKey("threats.id"), nullable=False)
    technology_id = Column(Integer, ForeignKey("technologies.id"), nullable=False)
    recommendation_text = Column(Text, nullable=False)
    priority = Column(String(20), nullable=False)

    threat = relationship("Threat", back_populates="recommendations")
    technology = relationship("Technology", back_populates="recommendations")


class AnalysisResult(Base):
    __tablename__ = "analysis_results"

    id = Column(Integer, primary_key=True, index=True)
    network_type = Column(String(80), nullable=False)
    threat_name = Column(String(120), nullable=False)
    probability = Column(Integer, nullable=False)
    confidentiality = Column(Integer, nullable=False)
    integrity = Column(Integer, nullable=False)
    availability = Column(Integer, nullable=False)
    asset_value = Column(Integer, nullable=False)
    cia_score = Column(Float, nullable=False)
    initial_risk = Column(Float, nullable=False)
    risk_level = Column(String(20), nullable=False)
    protection_effectiveness = Column(Float, nullable=False)
    residual_risk = Column(Float, nullable=False)
    risk_reduction = Column(Float, nullable=False)
    security_index = Column(Float, nullable=False)
    security_status = Column(String(40), nullable=False)
    priority_score = Column(Float, nullable=False)
    selected_technologies = Column(Text, nullable=False)
    recommendations = Column(Text, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
