"""系统使用统计 - 登录事件模型。"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, DateTime, String
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from fastapi_common.fastapi_common_web.models import BaseModel


class UsageLoginEvent(BaseModel):
    """登录事件明细表。"""

    __tablename__ = "usage_login_events"

    Id: Mapped[int] = mapped_column("id", BigInteger, primary_key=True, autoincrement=True)
    userId: Mapped[int | None] = mapped_column("user_id", BigInteger)
    sub: Mapped[str | None] = mapped_column(String(128))
    usernameSnapshot: Mapped[str | None] = mapped_column("username_snapshot", String(128))
    nickNameSnapshot: Mapped[str | None] = mapped_column("nick_name_snapshot", String(128))
    departmentNameSnapshot: Mapped[str | None] = mapped_column("department_name_snapshot", String(255))
    ouIdSnapshot: Mapped[str | None] = mapped_column("ou_id_snapshot", String(128))
    ouNameSnapshot: Mapped[str | None] = mapped_column("ou_name_snapshot", String(255))
    areaSnapshot: Mapped[str | None] = mapped_column("area_snapshot", String(64))
    loginTime: Mapped[datetime] = mapped_column("login_time", DateTime(timezone=True))
    loginResult: Mapped[str] = mapped_column("login_result", String(16))
    loginType: Mapped[str] = mapped_column("login_type", String(32))
    ipAddress: Mapped[str | None] = mapped_column("ip_address", String(64))
    userAgent: Mapped[str | None] = mapped_column("user_agent", String(1024))
    clientType: Mapped[str | None] = mapped_column("client_type", String(32))
    tokenJti: Mapped[str | None] = mapped_column("token_jti", String(128))
    failureReason: Mapped[str | None] = mapped_column("failure_reason", String(255))
    extra: Mapped[dict | None] = mapped_column(JSONB)
