"""Pydantic schemas for CuCu Note (moved to common.memos_core.schemas)."""

from __future__ import annotations

from datetime import datetime
from typing import Any, List, Optional

from pydantic import BaseModel, ConfigDict, EmailStr, Field
from pydantic.aliases import AliasChoices


class InstanceInfo(BaseModel):
  mode: Optional[str] = None
  version: Optional[str] = None


class InstanceUpdate(BaseModel):
  data_dir: Optional[str] = None


class InstanceSetting(BaseModel):
  # Stub fields for Connect compatibility
  allow_signup: bool = True
  disallow_login: bool = False
  additional_setting: dict | None = None


class AuthSignInRequest(BaseModel):
  """Accept both REST-style and Connect/proto-like payloads."""

  # REST-style
  email: Optional[EmailStr] = None
  password: Optional[str] = None

  # Connect/proto-like
  credentials: Optional[dict[str, Any]] = None


class AuthSignInResponse(BaseModel):
  accessToken: str = Field(validation_alias=AliasChoices("accessToken", "access_token"))
  tokenType: str = Field(default="bearer", validation_alias=AliasChoices("tokenType", "token_type"))

  model_config = ConfigDict(populate_by_name=True)


class AuthSignUpRequest(BaseModel):
  email: EmailStr
  password: str


class AuthSignUpResponse(BaseModel):
  user_id: str


class AuthMeResponse(BaseModel):
  id: str
  email: EmailStr


class UserBase(BaseModel):
  email: Optional[EmailStr] = None


class UserCreate(UserBase):
  password: str = ""


class UserUpdate(BaseModel):
  email: Optional[EmailStr] = None
  nickname: Optional[str] = None


class UserResponse(UserBase):
  id: str
  nickname: Optional[str] = None


class MemoBase(BaseModel):
  content: str
  visibility: Optional[str] = "PRIVATE"
  tags: List[str] = []


class MemoCreate(MemoBase):
  create_time: Optional[datetime] = None
  anonymous_id: Optional[str] = None
  anonymous_name: Optional[str] = None


class MemoUpdate(BaseModel):
  content: Optional[str] = None
  visibility: Optional[str] = None
  tags: Optional[List[str]] = None
  pinned: Optional[bool] = None
  row_status: Optional[str] = None


class MemoResponse(MemoBase):
  id: str
  uid: str = ""
  creator_id: str
  pinned: bool = False
  row_status: str = "NORMAL"
  create_time: Optional[str] = None
  update_time: Optional[str] = None
  display_time: Optional[str] = None
  parent: Optional[str] = None
  comment_count: int = 0

  model_config = ConfigDict(populate_by_name=True)


class MemoEmbeddingCreate(BaseModel):
  memoId: int
  content: str
  tags: List[str] = []
  dateKey: Optional[str] = None
  embedding: List[float]
  model: Optional[str] = "stub-embedding"


class MemoEmbeddingResponse(BaseModel):
  id: int
  memoId: int
  content: str
  tags: List[str]
  dateKey: Optional[str] = None
  dim: int
  model: str
  score: Optional[float] = None


class AttachmentResponse(BaseModel):
  id: str
  uid: str = ""
  filename: str
  url: str
  type: str = ""
  size: int = 0


class ShortcutCreate(BaseModel):
  name: str
  filter: str


class ShortcutUpdate(BaseModel):
  name: Optional[str] = None
  filter: Optional[str] = None


class ShortcutResponse(BaseModel):
  id: int
  name: str
  filter: str


class ListShortcutsResponse(BaseModel):
  shortcuts: List[ShortcutResponse] = []


class ActivityResponse(BaseModel):
  id: int
  type: str
  description: Optional[str] = None


class IdentityProviderCreate(BaseModel):
  name: str
  type: str


class IdentityProviderUpdate(BaseModel):
  name: Optional[str] = None
  config: Optional[dict] = None


class IdentityProviderResponse(BaseModel):
  id: int
  name: str
  type: str


