from pydantic import BaseModel, Field, validator
from typing import Optional
from datetime import datetime
import re


class AppUserRegisterSchema(BaseModel):
    """AppUser注册Schema - 只需要手机号"""
    phone: str = Field(..., description="手机号")
    
    @validator('phone')
    def validate_phone(cls, v):
        if not re.match(r'^1[3-9]\d{9}$', v):
            raise ValueError('手机号格式不正确')
        return v


class AppUserLoginSchema(BaseModel):
    """AppUser登录Schema"""
    phone: str = Field(..., description="手机号")
    password: str = Field(..., description="密码")


class AppUserJWTPayload(BaseModel):
    """AppUser JWT载荷"""
    user_id: int
    phone: str
    exp: datetime


class AppUserJWTOut(BaseModel):
    """AppUser JWT输出"""
    access_token: str
    token_type: str = "bearer"
    expires_in: int


class AppUserInfoOut(BaseModel):
    """AppUser信息输出"""
    id: int
    phone: str
    nickname: Optional[str] = None
    avatar: Optional[str] = None
    company_name: Optional[str] = None
    company_address: Optional[str] = None
    company_contact: Optional[str] = None
    company_phone: Optional[str] = None
    company_email: Optional[str] = None
    is_active: bool
    last_login: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime


class AppUserUpdateSchema(BaseModel):
    """AppUser更新Schema"""
    nickname: Optional[str] = Field(None, description="昵称")
    avatar: Optional[str] = Field(None, description="头像")
    company_name: Optional[str] = Field(None, description="公司名称")
    company_address: Optional[str] = Field(None, description="公司地址")
    company_contact: Optional[str] = Field(None, description="公司联系人")
    company_phone: Optional[str] = Field(None, description="公司电话")
    company_email: Optional[str] = Field(None, description="公司邮箱")


class AppUserChangePasswordSchema(BaseModel):
    """AppUser修改密码Schema"""
    old_password: str = Field(..., description="原密码")
    new_password: str = Field(..., description="新密码")