"""
认证模型定义

包含用户认证相关的 Pydantic 模型。
"""

from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field, ConfigDict


class TokenData(BaseModel):
    """Token 数据模型"""
    access_token: str
    refresh_token: str
    token_type: str = "bearer"
    expires_in: int = Field(description="Access token 过期时间（秒）")
    refresh_expires_in: int = Field(description="Refresh token 过期时间（秒）")


class TokenPayload(BaseModel):
    """JWT Token Payload"""
    sub: str = Field(description="用户 ID")
    exp: int = Field(description="过期时间戳")
    iat: int = Field(description="签发时间戳")
    type: str = Field(default="access", description="Token 类型: access 或 refresh")
    scopes: List[str] = Field(default=[], description="权限范围")
    jti: Optional[str] = Field(default=None, description="JWT ID，用于 token 撤销")
    sid: Optional[str] = Field(default=None, description="会话 ID")
    sfid: Optional[str] = Field(default=None, description="会话族 ID")


class TokenPair(BaseModel):
    """Token 对（access + refresh）"""
    access_token: str
    refresh_token: str
    token_type: str = "bearer"
    session_id: Optional[str] = None
    session_family_id: Optional[str] = None


class RefreshTokenRequest(BaseModel):
    """刷新 Token 请求"""
    refresh_token: str


class UserAuth(BaseModel):
    """用户认证信息"""
    id: str
    username: str
    email: Optional[str] = None
    avatar_url: Optional[str] = None
    is_active: bool = True
    is_superuser: bool = False
    permissions: List[str] = []
    roles: List[str] = []
    created_at: Optional[float] = None
    last_login: Optional[float] = None

    model_config = ConfigDict(from_attributes=True)


class UserLoginRequest(BaseModel):
    """用户登录请求"""
    username: str
    password: str


class UserRegisterRequest(BaseModel):
    """用户注册请求"""
    username: str
    email: str
    password: str
    password_confirm: str


class UserPasswordChangeRequest(BaseModel):
    """用户修改密码请求"""
    current_password: str
    new_password: str
    new_password_confirm: str


class OAuth2UserInfo(BaseModel):
    """OAuth2 用户信息"""
    provider: str
    provider_user_id: str
    email: Optional[str] = None
    username: Optional[str] = None
    avatar_url: Optional[str] = None
    raw_data: Dict[str, Any] = {}
