"""
# File       : api_注册.py
# Time       ：2024/8/22 18:41
# Author     ：xuewei zhang
# Email      ：shuiheyangguang@gmail.com
# version    ：python 3.12
# Description：
"""
from sqlalchemy.orm import selectinload
from fastapi import APIRouter, Depends
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.future import select
from sqlalchemy.ext.asyncio import AsyncSession
from user_auth_zxw.db.models import User
from user_auth_zxw.db.get_db import get_db
from user_auth_zxw.apis.api_登录注册.login import login_user_async
from user_auth_zxw.apis.api_用户权限_增加 import 分配或创建角色
from app_tools_zxw.Errors.api_errors import HTTPException_AppToolsSZXW, ErrorCode
from app_tools_zxw.msvc_user_auth.schemas import (返回_login,
                                                  请求_分配或创建角色,
                                                  请求_账号密码_注册,
                                                  请求_账号密码_登录)

router = APIRouter(prefix="/account/normal")


@router.post("/register/", response_model=返回_login)
async def 注册(user_create: 请求_账号密码_注册, db: AsyncSession = Depends(get_db)):
    # 检查用户名是否已存在
    result = await db.execute(select(User).filter(User.username == user_create.username))
    user = result.scalar_one_or_none()
    if user:
        raise HTTPException_AppToolsSZXW(error_code=ErrorCode.用户名已注册,
                                         detail="Username already registered",
                                         http_status_code=400)
    # 创建新用户并保存到数据库
    new_user = User(
        username=user_create.username,
        phone=user_create.username,
        email=user_create.username,
        openid=user_create.username,
        hashed_password=User.hash_password(user_create.password),
        notes="phone register"
    )
    db.add(new_user)
    await db.commit()
    await db.refresh(new_user)

    # 增加初始权限
    user_info = 请求_分配或创建角色(user_id=new_user.id, role_name=user_create.role_name, app_name=user_create.app_name)
    await 分配或创建角色(user_info, db)
    #
    return await login_user_async(new_user, db)


@router.post("/login/", response_model=返回_login)
async def 登录(info: 请求_账号密码_登录, db: AsyncSession = Depends(get_db)):
    result = await db.execute(select(User).options(selectinload(User.roles)).filter(User.username == info.username))
    user = result.scalar_one_or_none()
    print("apis/api_登录注册/api_注册.py: 登录(): user = ", user)

    if user is None or not user.verify_password(info.password):
        raise HTTPException_AppToolsSZXW(error_code=ErrorCode.无效的用户名或密码,
                                         detail="Invalid username or password",
                                         http_status_code=400)
    return await login_user_async(user, db)


@router.post("/login-form/", response_model=返回_login)
async def 登录_Form数据(login_info: OAuth2PasswordRequestForm = Depends(), db: AsyncSession = Depends(get_db)):
    #
    username = login_info.username
    password = login_info.password
    #
    result = await db.execute(select(User).options(selectinload(User.roles)).filter(User.username == username))
    user = result.scalar_one_or_none()
    print("apis/api_登录注册/api_注册.py: 登录(): user = ", user)

    if user is None or not user.verify_password(password):
        raise HTTPException_AppToolsSZXW(error_code=ErrorCode.无效的用户名或密码,
                                         detail="Invalid username or password",
                                         http_status_code=400)
    return await login_user_async(user, db)
