from datetime import timedelta
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from app.core.config import settings
from app.db.mongodb import db
from app.schemas.user import UserCreate, UserResponse, Token
from app.services.auth import authenticate_user, create_access_token, get_password_hash
from app.models.role import get_role_level

router = APIRouter()

@router.post("/register", response_model=UserResponse)
async def register(user_data: UserCreate):
    """注册新用户"""
    # 检查用户名是否已存在
    existing_user = await db.db.users.find_one({"username": user_data.username})
    if existing_user:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="用户名已存在"
        )
    
    # 检查邮箱是否已存在
    existing_email = await db.db.users.find_one({"email": user_data.email})
    if existing_email:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="邮箱已被注册"
        )
    
    # 创建新用户
    hashed_password = get_password_hash(user_data.password)
    user = {
        "username": user_data.username,
        "email": user_data.email,
        "hashed_password": hashed_password,
        "role": "user",         # 默认为普通用户
        "role_level": 1,          # 默认为 1 级
        "edition": "edu",        # 默认为教育版，可在后续管理员界面修改
    }
    
    result = await db.db.users.insert_one(user)
    
    # 获取创建的用户
    created_user = await db.db.users.find_one({"_id": result.inserted_id})
    
    return {
        "id": str(created_user["_id"]),
        "username": created_user["username"],
        "email": created_user["email"],
        "role": created_user.get("role", "user"),
        "role_level": created_user.get("role_level", 1),
        "edition": created_user.get("edition", "edu"),
    }

@router.post("/login", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    """用户登录"""
    user = await authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="用户名或密码错误",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    # 创建访问令牌（在载荷中加入用户的角色与版别信息）
    access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={
            "sub": str(user["_id"]),
            "role": user.get("role", "user"),
            "role_level": user.get("role_level", get_role_level(user.get("role"))),
            "edition": user.get("edition", "edu"),
        },
        expires_delta=access_token_expires
    )
    # 登录响应中回传基础用户信息，前端免一次 /me 调用；后端仍应以服务端鉴权为准
    return {
        "access_token": access_token,
        "token_type": "bearer",
        "role": user.get("role", "user"),
        "role_level": user.get("role_level", get_role_level(user.get("role"))),
        "edition": user.get("edition", "edu"),
    }