"""RBAC 管理 VO。"""

from __future__ import annotations

from pydantic import BaseModel, Field


class RoleVO(BaseModel):
    """角色信息。"""

    id: int = Field(..., description="角色ID")
    role_key: str = Field(..., description="角色标识")
    role_name: str = Field(..., description="角色名称")
    data_scope: str = Field(..., description="数据范围")
    description: str = Field("", description="角色描述")
    parent_role_id: int | None = Field(None, description="父角色ID")
    priority: int = Field(0, description="优先级")
    is_system: bool = Field(False, description="是否系统角色")
    created_at: str | None = Field(None, description="创建时间")
    updated_at: str | None = Field(None, description="更新时间")


class RoleListVO(BaseModel):
    """角色列表分页。"""

    total: int = Field(0, description="总数")
    page: int = Field(1, description="页码")
    page_size: int = Field(50, description="分页大小")
    items: list[RoleVO] = Field(default_factory=list, description="角色列表")


class UserRoleVO(BaseModel):
    """用户角色。"""

    role_id: int = Field(..., description="角色ID")
    role_key: str = Field(..., description="角色标识")
    role_name: str = Field(..., description="角色名称")


class UserVO(BaseModel):
    """用户信息。"""

    id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    nick_name: str = Field(..., description="姓名")
    phone_number: str | None = Field(None, description="手机号")
    email: str | None = Field(None, description="邮箱")
    area: str | None = Field(None, description="地区")
    ou_name: str | None = Field(None, description="组织名称")
    ou_id: str | None = Field(None, description="组织ID")
    status: int = Field(0, description="状态")
    is_leader: bool = Field(False, description="是否负责人")
    roles: list[UserRoleVO] = Field(default_factory=list, description="角色列表")
    tenant_name: str | None = Field(None, description="租户名称")
    dep_name: str | None = Field(None, description="部门名称")
    dep_short_name: str | None = Field(None, description="部门简称")


class UserListVO(BaseModel):
    """用户列表分页。"""

    total: int = Field(0, description="总数")
    page: int = Field(1, description="页码")
    page_size: int = Field(50, description="分页大小")
    items: list[UserVO] = Field(default_factory=list, description="用户列表")


class OrganizationPathVO(BaseModel):
    """用户组织路径。"""

    tenant_name: str = Field("", description="租户名称")
    dep_name: str = Field("", description="部门名称")
    dep_short_name: str = Field("", description="部门简称")
    ou_name: str = Field("", description="组织名称")


class OrganizationTreeUserVO(BaseModel):
    """组织树节点中的用户。"""

    id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    nick_name: str = Field(..., description="姓名")
    area: str | None = Field(None, description="地区")
    ou_id: str = Field("", description="组织ID")
    ou_name: str = Field("", description="组织名称")
    is_leader: bool = Field(False, description="是否负责人")
    status: int = Field(0, description="状态")
    tenant_name: str | None = Field(None, description="租户名称")
    dep_name: str | None = Field(None, description="部门名称")
    dep_short_name: str | None = Field(None, description="部门简称")
    email: str | None = Field(None, description="邮箱")
    phone_number: str | None = Field(None, description="手机号")
    organization_path: OrganizationPathVO | None = Field(None, description="组织路径")


class OrganizationNodeVO(BaseModel):
    """组织树节点。"""

    ou_id: str = Field(..., description="节点ID")
    ou_name: str = Field(..., description="节点名称")
    parent_ou_id: str | None = Field(None, description="父节点ID")
    level: int = Field(1, description="层级")
    children: list["OrganizationNodeVO"] = Field(default_factory=list, description="子节点")
    users: list[OrganizationTreeUserVO] = Field(default_factory=list, description="节点用户")


class OrganizationTreeVO(BaseModel):
    """组织树响应。"""

    organizations: list[OrganizationNodeVO] = Field(default_factory=list, description="组织树")
    total_organizations: int = Field(0, description="组织节点总数")
    total_users: int = Field(0, description="用户总数")


class RoutePermissionVO(BaseModel):
    """路由关联的 API 权限。"""

    id: int = Field(..., description="权限ID")
    permission_key: str = Field(..., description="权限键")
    display_name: str | None = Field(None, description="显示名称")
    api_method: str | None = Field(None, description="请求方法")
    api_path: str | None = Field(None, description="接口路径")
    route_id: int | None = Field(None, description="主路由ID")
    related_routes: list[int] | None = Field(None, description="共享路由ID列表")
    is_shared: bool = Field(False, description="是否共享权限")


class RouteVO(BaseModel):
    """RBAC 管理路由。"""

    id: int = Field(..., description="路由ID")
    route_path: str = Field(..., description="路径")
    route_name: str = Field(..., description="名称")
    route_title: str = Field(..., description="标题")
    component: str | None = Field(None, description="组件")
    parent_id: int | None = Field(None, description="父ID")
    icon: str | None = Field(None, description="图标")
    sort_order: int = Field(0, description="排序")
    is_hidden: bool = Field(False, description="是否隐藏")
    is_cache: bool = Field(True, description="是否缓存")
    status: int = Field(0, description="状态")
    enabled: bool = Field(False, description="角色是否启用该路由")
    permissions: list[RoutePermissionVO] = Field(default_factory=list, description="关联权限")
    children: list["RouteVO"] | None = Field(None, description="子路由")


class RoleRoutesVO(BaseModel):
    """角色路由树响应。"""

    role_id: int = Field(..., description="角色ID")
    routes: list[RouteVO] = Field(default_factory=list, description="路由树")


class RoleRouteUpdateResultVO(BaseModel):
    """角色路由更新结果。"""

    role_id: int = Field(..., description="角色ID")
    enabled_count: int = Field(0, description="启用数量")
    disabled_count: int = Field(0, description="禁用数量")
    inserted_count: int = Field(0, description="新增数量")
    route_ids: list[int] = Field(default_factory=list, description="启用路由ID")


class RolePermissionDetailVO(BaseModel):
    """角色权限详情。"""

    id: int = Field(..., description="角色权限关联ID")
    permission_id: int = Field(..., description="权限ID")
    permission_key: str = Field(..., description="权限键")
    display_name: str | None = Field(None, description="显示名")
    grant_type: str = Field("GRANT", description="授权类型")
    data_scope: str | None = Field(None, description="数据范围")


class RolePermissionsVO(BaseModel):
    """角色权限响应。"""

    role_id: int = Field(..., description="角色ID")
    permissions: list[RolePermissionDetailVO] = Field(default_factory=list, description="权限列表")


class RoleAccessSaveVO(BaseModel):
    """角色菜单与接口权限联合保存响应。"""

    role_id: int = Field(..., description="角色ID")
    route_result: RoleRouteUpdateResultVO = Field(..., description="菜单保存结果")
    permission_result: RolePermissionsVO = Field(..., description="接口权限保存结果")


class UserRolesVO(BaseModel):
    """用户角色响应。"""

    user_id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    roles: list[RoleVO] = Field(default_factory=list, description="角色列表")


class RoutePermissionsVO(BaseModel):
    """路由权限响应。"""

    route_id: int = Field(..., description="路由ID")
    route_path: str = Field(..., description="路由路径")
    route_title: str = Field(..., description="路由标题")
    permissions: list[RoutePermissionVO] = Field(default_factory=list, description="权限列表")


RouteVO.model_rebuild()
OrganizationNodeVO.model_rebuild()
