import uuid
from sqlalchemy import Column, String, DateTime, Text, Index
from datetime import datetime
from app.models.base import BaseModel


class TokenBlacklist(BaseModel):
    """Token blacklist for cross-browser/device logout"""
    __tablename__ = "token_blacklist"

    id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    token_jti = Column(String, nullable=False, unique=True, index=True)  # JWT ID from token
    user_id = Column(String, nullable=False, index=True)
    revoked_at = Column(DateTime, nullable=False, default=datetime.utcnow)
    expires_at = Column(DateTime, nullable=False)  # Token expiration time
    reason = Column(String)  # logout, password_change, admin_revoke, etc.
    ip_address = Column(String)
    user_agent = Column(Text)

    __table_args__ = (
        Index('idx_token_jti_lookup', 'token_jti'),
        Index('idx_user_revoked', 'user_id', 'revoked_at'),
        Index('idx_expires_cleanup', 'expires_at'),
    )

    def __repr__(self):
        return f"<TokenBlacklist(jti={self.token_jti[:8]}..., user={self.user_id}, revoked={self.revoked_at})>"
