from pydantic import BaseModel, Field

class SetUserRoleRequest(BaseModel):
    role: str
    sub_role: str | None = None
    org_id: str | None = None
    region_id: str | None = None

class SetUserStatusRequest(BaseModel):
    status: str = "active"  # active | disabled

class UpdateUserRequest(BaseModel):
    nickname: str | None = None
    role: str | None = None
    sub_role: str | None = None
    org_id: str | None = None
    region_id: str | None = None

class UserCreateRequest(BaseModel):
    username: str          # 手机号
    password: str
    nickname: str = ""
    role: str = "opc_member"
    sub_role: str | None = None
    org_id: str | None = None
    region_id: str | None = None



class RolePermissionRequest(BaseModel):
    permissions: list[str] = []


async def _validate_role_assignment(db: Database, actor: dict, role: str, sub_role: str | None) -> None:
    """白名单 + 提权防护：拒绝非法角色组合，及无权者创建/提升特权角色。"""
    key = role if not sub_role else f"{role}|{sub_role}"
    if not await db.roles.role_exists(role):
        raise HTTPException(status_code=400, detail=f"无效角色: {role}")
    if sub_role and not await db.roles.role_exists(f"{role}|{sub_role}"):
        raise HTTPException(status_code=400, detail=f"无效角色组合: {key}")
    # 目标角色若授予「配置角色权限」，仅持该权限者（超管）可创建/分配
    if "action:role.grant_perm" in await db.roles.permissions_for(role, sub_role) and \
            "action:role.grant_perm" not in actor.get("permissions", []):
        raise HTTPException(
            status_code=403,
            detail="无权创建/分配特权角色（仅超级管理员）",
        )



