"""
API Key model for multi-tenant system
"""
from datetime import datetime
from typing import Dict, Any, Optional
import uuid
from sqlalchemy import Boolean, DateTime, Integer, String, func, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .base import Base
# Import for type hints
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .client import Client
class APIKey(Base):
"""Model for API keys (multiple per client)"""
__tablename__ = "api_keys"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
client_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("clients.id"), nullable=False)
key_value: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False) # "Production", "Dev", etc.
created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now(), nullable=False)
expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
# Relationships
client: Mapped["Client"] = relationship("Client", back_populates="api_keys")
def __repr__(self) -> str:
return f"<APIKey(id={self.id}, name='{self.name}', key='{self.key_value[:8]}...', active={self.is_active})>"
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary for API responses"""
return {
"id": self.id,
"client_id": str(self.client_id),
"key_value": self.key_value,
"name": self.name,
"created_at": self.created_at.isoformat(),
"expires_at": self.expires_at.isoformat() if self.expires_at else None,
"is_active": self.is_active
}