"""
Admin session tracking model for security monitoring.
Separate from accountant sessions to maintain clear separation of concerns.
"""
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, JSON, Index, Text
from sqlalchemy.orm import relationship
from datetime import datetime
from ...shared.config.database import Base


class AdminSession(Base):
    """Track admin sessions for security monitoring."""
    __tablename__ = 'admin_sessions'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # User reference
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    
    # Session details
    session_token = Column(String(255), unique=True, nullable=False, index=True)
    ip_address = Column(String(45), nullable=True)  # IPv6 compatible
    user_agent = Column(Text, nullable=True)
    device_fingerprint = Column(String(255), nullable=True)
    
    # Location (if available)
    country = Column(String(2), nullable=True)  # ISO country code
    city = Column(String(100), nullable=True)
    
    # Session status
    is_active = Column(Boolean, default=True, nullable=False, index=True)
    last_activity = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Step-up authentication
    step_up_authenticated = Column(Boolean, default=False, nullable=False)
    step_up_expires_at = Column(DateTime, nullable=True)
    
    # Session metadata
    session_metadata = Column(JSON, nullable=True)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    expires_at = Column(DateTime, nullable=False, index=True)
    
    # Relationships
    user = relationship('User', foreign_keys=[user_id])
    
    # Indexes
    __table_args__ = (
        Index('idx_admin_session_user_active', 'user_id', 'is_active', 'last_activity'),
        Index('idx_admin_session_expires', 'expires_at', 'is_active'),
    )


class AdminActivityLog(Base):
    """Log admin activity for security monitoring and audit trails."""
    __tablename__ = 'admin_activity_logs'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # User reference
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    session_id = Column(Integer, ForeignKey('admin_sessions.id'), nullable=True, index=True)
    
    # Activity details
    activity_type = Column(String(50), nullable=False, index=True)  # 'login', 'user_management', 'system_config', etc.
    activity_description = Column(Text, nullable=False)
    
    # Location and device
    ip_address = Column(String(45), nullable=True)
    user_agent = Column(Text, nullable=True)
    country = Column(String(2), nullable=True)
    city = Column(String(100), nullable=True)
    
    # Risk indicators
    risk_level = Column(String(20), default='low', nullable=False)  # 'low', 'medium', 'high', 'critical'
    is_unusual = Column(Boolean, default=False, nullable=False, index=True)
    
    # Additional context
    activity_metadata = Column(JSON, nullable=True)
    
    # Timestamp
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Relationships
    user = relationship('User', foreign_keys=[user_id])
    session = relationship('AdminSession', foreign_keys=[session_id])
    
    # Indexes
    __table_args__ = (
        Index('idx_admin_activity_user_date', 'user_id', 'created_at'),
        Index('idx_admin_activity_unusual', 'is_unusual', 'risk_level', 'created_at'),
        Index('idx_admin_activity_type', 'activity_type', 'created_at'),
    )

