"""Schemas for the lightweight Memory System API."""
from __future__ import annotations

from typing import Any, Literal

from pydantic import BaseModel, Field


OperationStatus = Literal["success", "partial_success", "failed"]
MemoryWriteMode = Literal["create", "replace", "append"]


class MessageIngestRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)
    session_id: str = Field(min_length=1)
    user_message: str | None = None
    assistant_message: str | None = None
    timestamp: int | None = None
    metadata: dict[str, Any] = Field(default_factory=dict)


class SessionUserRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)


class TaskStatusRequest(SessionUserRequest):
    session_id: str | None = Field(default=None, min_length=1)


class SearchRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)
    session_id: str | None = None
    query: str = Field(min_length=1)
    use_llm: bool = False
    limit: int = Field(default=10, ge=1, le=100)
    level: int = Field(default=2, ge=0)
    score_threshold: float = Field(default=0.8, ge=0, le=1)
    target_uri: str = Field(default="viking://user/memories", min_length=1)


class SessionContextRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)
    query: str = Field(min_length=1)
    limit: int = Field(default=10, ge=1, le=100)


class ProfileRequest(BaseModel):
    user_key: str = Field(min_length=1)
    query: str = Field(default="用户画像", min_length=1)
    limit: int = Field(default=10, ge=1, le=100)
    level: int = Field(default=2, ge=0)


class MemoryWriteRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)
    uri: str = Field(min_length=1)
    content: str
    mode: MemoryWriteMode = "create"
    wait: bool = True


class ResourceUploadRequest(BaseModel):
    user_id: str = Field(min_length=1)
    user_key: str = Field(min_length=1)
    path: str = Field(min_length=1)
    to: str = Field(min_length=1)
    reason: str | None = None
    wait: bool = True
    directly_upload_media: bool = True


class BackendStatus(BaseModel):
    status: OperationStatus
    result: Any = None
    error: str | None = None


class UserCreateRequest(BaseModel):
    user_id: str = Field(min_length=1)


class AccountResponse(BaseModel):
    status: OperationStatus
    account: Any = None
    backends: dict[str, BackendStatus]


class MessageIngestResponse(BaseModel):
    status: OperationStatus
    message_count: int
    backends: dict[str, BackendStatus]


class CommitResponse(BaseModel):
    status: OperationStatus
    backends: dict[str, BackendStatus]


class ExtractResponse(BaseModel):
    status: OperationStatus
    backends: dict[str, BackendStatus]


class SearchResponse(BaseModel):
    status: OperationStatus
    items: list[dict[str, Any]] = Field(default_factory=list)
    backends: dict[str, BackendStatus]


class SessionContextResponse(BaseModel):
    status: OperationStatus
    context: dict[str, Any] | None = None
    items: list[dict[str, Any]] = Field(default_factory=list)
    backends: dict[str, BackendStatus]


class ProfileResponse(BaseModel):
    status: OperationStatus
    profile: Any = None
    items: list[dict[str, Any]] = Field(default_factory=list)
    backends: dict[str, BackendStatus]


class MemoryOperationResponse(BaseModel):
    status: OperationStatus
    memory: Any = None
    backends: dict[str, BackendStatus]


class ResourceMutationResponse(BaseModel):
    status: OperationStatus
    resource: Any = None
    backends: dict[str, BackendStatus]
