"""User models for vikdata API."""

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, EmailStr, Field

from vikdata.models.role import RoleResponse


class UserBase(BaseModel):
    """Base user model with common fields."""

    email: EmailStr = Field(..., description="User email address")
    display_name: str | None = Field(default=None, description="User display name")
    avatar_url: str | None = Field(default=None, description="User avatar URL")
    is_active: bool = Field(default=True, description="Whether user is active")
    metadata: dict | None = Field(default=None, description="Additional user metadata")


class UserCreate(UserBase):
    """User creation request."""

    password: str | None = Field(default=None, min_length=8, description="User password")
    roles: list[str] | None = Field(default=None, description="Role slugs to assign")


class UserUpdate(BaseModel):
    """User update request."""

    display_name: str | None = Field(default=None, description="User display name")
    avatar_url: str | None = Field(default=None, description="User avatar URL")
    is_active: bool | None = Field(default=None, description="Whether user is active")
    metadata: dict | None = Field(default=None, description="Additional user metadata")


class UserResponse(BaseModel):
    """User response with full details."""

    id: UUID = Field(..., description="User ID")
    tenant_id: UUID = Field(..., description="Tenant ID")
    email: str = Field(..., description="User email address")
    display_name: str | None = Field(default=None, description="User display name")
    avatar_url: str | None = Field(default=None, description="User avatar URL")
    is_active: bool = Field(..., description="Whether user is active")
    metadata: dict | None = Field(default=None, description="Additional user metadata")
    email_verified_at: datetime | None = Field(
        default=None, description="Email verification timestamp"
    )
    last_login_at: datetime | None = Field(default=None, description="Last login timestamp")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")
    roles: list[RoleResponse] | None = Field(default=None, description="User roles")


class UserListResponse(BaseModel):
    """Paginated list of users."""

    data: list[UserResponse] = Field(..., description="List of users")
    meta: dict = Field(..., description="Pagination metadata")


class UserListParams(BaseModel):
    """Query parameters for listing users."""

    page: int | None = Field(default=1, ge=1, description="Page number")
    per_page: int | None = Field(default=20, ge=1, le=100, description="Items per page")
    search: str | None = Field(default=None, description="Search by email or name")
    role: str | None = Field(default=None, description="Filter by role slug")
    is_active: bool | None = Field(default=None, description="Filter by status")
