from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, JSON, Enum, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base

class DataSubjectRequestType(str, enum.Enum):
    access = 'access'  # Right to access
    rectification = 'rectification'  # Right to rectification
    erasure = 'erasure'  # Right to erasure (right to be forgotten)
    portability = 'portability'  # Right to data portability
    restriction = 'restriction'  # Right to restriction of processing
    objection = 'objection'  # Right to object

class DataSubjectRequestStatus(str, enum.Enum):
    pending = 'pending'
    in_progress = 'in_progress'
    completed = 'completed'
    rejected = 'rejected'
    cancelled = 'cancelled'

class DataSubjectRequest(Base):
    __tablename__ = 'data_subject_requests'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=True, index=True)
    email = Column(String(255), nullable=False, index=True)
    request_type = Column(Enum(DataSubjectRequestType), nullable=False, index=True)
    status = Column(Enum(DataSubjectRequestStatus), nullable=False, default=DataSubjectRequestStatus.pending, index=True)
    
    # Request details
    description = Column(Text, nullable=True)
    verification_token = Column(String(100), nullable=True, unique=True, index=True)
    verified = Column(Boolean, nullable=False, default=False)
    verified_at = Column(DateTime, nullable=True)
    
    # Processing
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True)
    notes = Column(Text, nullable=True)
    response_data = Column(JSON, nullable=True)  # For access requests, store the data
    
    # Completion
    completed_at = Column(DateTime, nullable=True)
    completed_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Metadata
    ip_address = Column(String(45), nullable=True)
    user_agent = Column(String(500), 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)
    
    # Relationships
    user = relationship('User', foreign_keys=[user_id])
    assignee = relationship('User', foreign_keys=[assigned_to])
    completer = relationship('User', foreign_keys=[completed_by])

class DataRetentionPolicy(Base):
    __tablename__ = 'data_retention_policies'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    data_type = Column(String(100), nullable=False, unique=True)  # e.g., 'user_data', 'booking_data', 'payment_data'
    retention_days = Column(Integer, nullable=False)  # Days to retain data
    auto_delete = Column(Boolean, nullable=False, default=False)
    description = Column(Text, nullable=True)
    is_active = Column(Boolean, nullable=False, default=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

class ConsentRecord(Base):
    __tablename__ = 'consent_records'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    consent_type = Column(String(100), nullable=False, index=True)  # 'marketing', 'analytics', 'cookies', etc.
    granted = Column(Boolean, nullable=False, default=False)
    granted_at = Column(DateTime, nullable=True)
    revoked_at = Column(DateTime, nullable=True)
    ip_address = Column(String(45), nullable=True)
    user_agent = Column(String(500), nullable=True)
    version = Column(String(50), nullable=True)  # Policy version when consent was given
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    user = relationship('User')

