"""
GDPR Data Retention Policy Model.
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum, JSON, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime, timedelta
import enum
from ...shared.config.database import Base

class RetentionRule(Base):
    """Data retention rules for different data types."""
    __tablename__ = 'retention_rules'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Rule details
    data_category = Column(String(100), nullable=False, unique=True, index=True)  # user_data, booking_data, payment_data, etc.
    retention_period_days = Column(Integer, nullable=False)  # Number of days to retain
    retention_period_months = Column(Integer, nullable=True)  # Alternative: months
    retention_period_years = Column(Integer, nullable=True)  # Alternative: years
    
    # Legal basis
    legal_basis = Column(Text, nullable=True)  # Why we retain for this period
    legal_requirement = Column(Text, nullable=True)  # Specific legal requirement if any
    
    # Action after retention
    action_after_retention = Column(String(50), nullable=False, default='anonymize')  # delete, anonymize, archive
    
    # Conditions
    conditions = Column(JSON, nullable=True)  # Additional conditions (e.g., active bookings)
    
    # Status
    is_active = Column(Boolean, default=True, nullable=False, index=True)
    
    # Metadata
    description = Column(Text, nullable=True)
    created_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    creator = relationship('User', foreign_keys=[created_by])

class DataRetentionLog(Base):
    """Log of data retention actions performed."""
    __tablename__ = 'data_retention_logs'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Retention action
    retention_rule_id = Column(Integer, ForeignKey('retention_rules.id'), nullable=False, index=True)
    data_category = Column(String(100), nullable=False, index=True)
    action_taken = Column(String(50), nullable=False)  # deleted, anonymized, archived
    
    # Affected records
    records_affected = Column(Integer, nullable=False, default=0)
    affected_ids = Column(JSON, nullable=True)  # IDs of affected records (for audit)
    
    # Execution
    executed_by = Column(Integer, ForeignKey('users.id'), nullable=True)  # System or admin
    executed_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Results
    success = Column(Boolean, default=True, nullable=False)
    error_message = Column(Text, nullable=True)
    
    # Metadata
    extra_metadata = Column(JSON, nullable=True)
    
    # Relationships
    retention_rule = relationship('RetentionRule', foreign_keys=[retention_rule_id])
    executor = relationship('User', foreign_keys=[executed_by])

