"""
GDPR Data Breach Notification Model (Article 33-34 GDPR).
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum, JSON, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base

class BreachType(str, enum.Enum):
    """Types of data breaches."""
    confidentiality = "confidentiality"  # Unauthorized disclosure
    integrity = "integrity"  # Unauthorized alteration
    availability = "availability"  # Unauthorized destruction or loss

class BreachStatus(str, enum.Enum):
    """Status of breach notification."""
    detected = "detected"
    investigating = "investigating"
    contained = "contained"
    reported_to_authority = "reported_to_authority"
    notified_data_subjects = "notified_data_subjects"
    resolved = "resolved"

class DataBreach(Base):
    """Data breach notification record (Articles 33-34 GDPR)."""
    __tablename__ = 'data_breaches'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Breach details
    breach_type = Column(Enum(BreachType), nullable=False, index=True)
    status = Column(Enum(BreachStatus), default=BreachStatus.detected, nullable=False, index=True)
    
    # Description
    description = Column(Text, nullable=False)  # Nature of the breach
    affected_data_categories = Column(JSON, nullable=True)  # Categories of personal data affected
    affected_data_subjects = Column(JSON, nullable=True)  # Approximate number of affected individuals
    
    # Timeline
    detected_at = Column(DateTime, nullable=False, index=True)
    occurred_at = Column(DateTime, nullable=True)  # When breach occurred (if known)
    contained_at = Column(DateTime, nullable=True)
    
    # Notification
    reported_to_authority_at = Column(DateTime, nullable=True)  # Article 33 - 72 hours
    authority_reference = Column(String(255), nullable=True)  # Reference from supervisory authority
    notified_data_subjects_at = Column(DateTime, nullable=True)  # Article 34 - without undue delay
    notification_method = Column(String(100), nullable=True)  # email, public_notice, etc.
    
    # Risk assessment
    likely_consequences = Column(Text, nullable=True)
    measures_proposed = Column(Text, nullable=True)  # Measures to address the breach
    risk_level = Column(String(20), nullable=True)  # low, medium, high
    
    # Reporting
    reported_by = Column(Integer, ForeignKey('users.id'), nullable=False)  # Who detected/reported
    investigated_by = Column(Integer, ForeignKey('users.id'), nullable=True)  # DPO or responsible person
    
    # Additional details
    extra_metadata = Column(JSON, nullable=True)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    reporter = relationship('User', foreign_keys=[reported_by])
    investigator = relationship('User', foreign_keys=[investigated_by])

