from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, EmailStr, Field, validator


# 用户基础模式
class UserBase(BaseModel):
    username: Optional[str] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    user_type: Optional[str] = None
    real_name: Optional[str] = None
    nickname: Optional[str] = None
    avatar_url: Optional[str] = None
    gender: Optional[str] = None
    birthday: Optional[datetime] = None


# 创建用户请求模式
class UserCreate(BaseModel):
    username: str = Field(..., min_length=3, max_length=50)
    password: str = Field(..., min_length=6)
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    real_name: Optional[str] = None
    nickname: Optional[str] = None
    user_type: str = "farmer"


# 更新用户请求模式
class UserUpdate(BaseModel):
    nickname: Optional[str] = None
    avatar_url: Optional[str] = None
    gender: Optional[str] = None
    birthday: Optional[datetime] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None


# 用户登录请求模式
class UserLogin(BaseModel):
    username: str
    password: str


# 密码更改请求模式
class PasswordChange(BaseModel):
    current_password: str
    new_password: str = Field(..., min_length=6)


# 用户响应模式
class UserResponse(UserBase):
    id: int
    status: str
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None

    class Config:
        orm_mode = True


# 带令牌的用户响应模式
class UserWithToken(BaseModel):
    user: UserResponse
    token: str
    refresh_token: Optional[str] = None


# 令牌响应模式
class Token(BaseModel):
    access_token: str
    refresh_token: str
    token_type: str = "bearer"


# 令牌数据模式
class TokenPayload(BaseModel):
    sub: Optional[int] = None
    exp: Optional[int] = None
    type: Optional[str] = None


# 管理员基础模式
class AdminBase(BaseModel):
    username: str
    email: Optional[EmailStr] = None
    nickname: Optional[str] = None
    avatar: Optional[str] = None
    role: str = "admin"


# 创建管理员请求模式
class AdminCreate(AdminBase):
    password: str = Field(..., min_length=6)


# 更新管理员请求模式
class AdminUpdate(BaseModel):
    nickname: Optional[str] = None
    avatar: Optional[str] = None
    email: Optional[EmailStr] = None
    role: Optional[str] = None
    status: Optional[str] = None


# 管理员响应模式
class AdminResponse(AdminBase):
    id: int
    status: str
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None

    class Config:
        orm_mode = True


# 带令牌的管理员响应模式
class AdminWithToken(BaseModel):
    admin: AdminResponse
    token: str
    refresh_token: Optional[str] = None


# 微信登录请求模式
class WechatLogin(BaseModel):
    code: str
    user_info: Optional[dict] = None


# 分页响应模式
class PaginationResponse(BaseModel):
    page: int
    page_size: int
    total: int
    total_pages: int


# 用户列表响应模式
class UserListResponse(BaseModel):
    users: List[UserResponse]
    pagination: PaginationResponse


# 用户统计响应模式
class UserStatistics(BaseModel):
    total_users: int
    farmers: int
    merchants: int
    admins: int
    active_users: int
    inactive_users: int
    date: Optional[datetime] = None


# 批量更新用户状态请求模式
class BatchUserStatusUpdate(BaseModel):
    user_ids: List[int]
    status: str

    @validator("status")
    def validate_status(cls, v):
        if v not in ["active", "inactive"]:
            raise ValueError("状态必须是 'active' 或 'inactive'")
        return v