"""Role and permission schemas for RBAC."""

from uuid import UUID

from pydantic import BaseModel, Field


class PermissionResponse(BaseModel):
    """Permission response model."""

    id: UUID = Field(..., description="Permission ID")
    name: str = Field(..., description="Permission name (e.g., 'users:read')")
    description: str | None = Field(default=None, description="Permission description")


class RoleBase(BaseModel):
    """Base role model."""

    name: str = Field(..., min_length=1, max_length=100, description="Role display name")
    slug: str = Field(..., min_length=1, max_length=100, description="Role slug/identifier")
    description: str | None = Field(default=None, description="Role description")


class RoleCreate(RoleBase):
    """Role creation request."""

    permission_ids: list[UUID] = Field(
        default_factory=list, description="Initial permission IDs to assign"
    )


class RoleUpdate(BaseModel):
    """Role update request."""

    name: str | None = Field(
        default=None, min_length=1, max_length=100, description="Role display name"
    )
    description: str | None = Field(default=None, description="Role description")


class RoleResponse(RoleBase):
    """Role response model."""

    id: UUID = Field(..., description="Role ID")
    tenant_id: UUID | None = Field(default=None, description="Tenant ID (null for system roles)")
    is_system: bool = Field(..., description="Whether this is a system role")
    permissions: list[PermissionResponse] = Field(
        default_factory=list, description="Permissions assigned to this role"
    )


class RoleListResponse(BaseModel):
    """List of roles response."""

    data: list[RoleResponse] = Field(..., description="List of roles")
    total: int = Field(..., description="Total number of roles")


class RolePermissionRequest(BaseModel):
    """Request to assign permissions to a role."""

    permission_ids: list[UUID] = Field(..., min_length=1, description="Permission IDs to assign")


class UserRoleRequest(BaseModel):
    """Request to assign a role to a user."""

    role_id: UUID = Field(..., description="Role ID to assign")


class UserRoleResponse(BaseModel):
    """User role assignment response."""

    id: UUID = Field(..., description="Assignment ID")
    assignee_id: UUID = Field(..., description="User ID")
    role_id: UUID = Field(..., description="Role ID")
    role: RoleResponse = Field(..., description="Role details")
