"""
GDPR compliance models for data export and deletion requests.
"""
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 GDPRRequestType(str, enum.Enum):
    data_export = "data_export"
    data_deletion = "data_deletion"
    data_rectification = "data_rectification"
    consent_withdrawal = "consent_withdrawal"

class GDPRRequestStatus(str, enum.Enum):
    pending = "pending"
    processing = "processing"
    completed = "completed"
    rejected = "rejected"
    cancelled = "cancelled"

class GDPRRequest(Base):
    __tablename__ = 'gdpr_requests'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    request_type = Column(Enum(GDPRRequestType), nullable=False, index=True)
    status = Column(Enum(GDPRRequestStatus), default=GDPRRequestStatus.pending, nullable=False, index=True)
    
    # User making the request (nullable for anonymous users)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=True, index=True)
    user_email = Column(String(255), nullable=False)  # Required: email for anonymous or registered users
    is_anonymous = Column(Boolean, default=False, nullable=False, index=True)  # Flag for anonymous requests
    
    # Request details
    request_data = Column(JSON, nullable=True)  # Additional request parameters
    verification_token = Column(String(255), nullable=True, unique=True, index=True)
    verified_at = Column(DateTime, nullable=True)
    
    # Processing
    processed_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    processed_at = Column(DateTime, nullable=True)
    processing_notes = Column(Text, nullable=True)
    
    # Export/deletion details
    export_file_path = Column(String(500), nullable=True)  # Path to exported data file
    deletion_log = Column(JSON, nullable=True)  # Log of what was deleted
    
    # Metadata
    ip_address = Column(String(45), nullable=True)
    user_agent = Column(String(255), nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    expires_at = Column(DateTime, nullable=True)  # For export links expiration
    
    # Relationships
    user = relationship('User', foreign_keys=[user_id])
    processor = relationship('User', foreign_keys=[processed_by])

