from datetime import datetime

from sqlalchemy import TIMESTAMP, ForeignKey, Integer, String, text
from sqlalchemy.orm import Mapped, mapped_column

from src.core.database import Base
from src.core.settings import settings


class RefreshTokensORM(Base):
    __tablename__ = "refresh_tokens"
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    token: Mapped[str] = mapped_column(String(255), nullable=False)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"))
    fingerprint: Mapped[str] = mapped_column(String(255), nullable=False)
    expired_at: Mapped[datetime] = mapped_column(
        TIMESTAMP(timezone=True),
        server_default=text(
            f"datetime('now', '+{settings.refresh_token.expire_days} days')"
        ),
    )
