import uuid
from datetime import datetime

from sqlalchemy import Column, String, Text, Boolean, DateTime, Enum
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship

from app.database import Base
from app.models.enums import TechniqueStatus


class Technique(Base):
    """
    MITRE ATT&CK Technique model.
    
    Represents an attack technique from the MITRE ATT&CK framework,
    including its coverage status and associated tests.
    """
    __tablename__ = "techniques"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    mitre_id = Column(String, unique=True, nullable=False)  # e.g., "T1059.001"
    name = Column(String, nullable=False)
    description = Column(Text, nullable=True)
    tactic = Column(String, nullable=True)
    platforms = Column(JSONB, nullable=True, default=[])
    mitre_version = Column(String, nullable=True)
    mitre_last_modified = Column(DateTime, nullable=True)
    is_subtechnique = Column(Boolean, default=False)
    parent_mitre_id = Column(String, nullable=True)
    status_global = Column(
        Enum(TechniqueStatus, name="techniquestatus"),
        default=TechniqueStatus.not_evaluated
    )
    review_required = Column(Boolean, default=False)
    last_review_date = Column(DateTime, nullable=True)

    # Relationships
    tests = relationship("Test", back_populates="technique")
