"""
Pydantic schemas for Team workspace (Draft → Main flow).
Includes: comments, review flow, reactions, user profile resolution.
"""

from __future__ import annotations

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field


# ====================== USER PROFILE ======================

class UserProfileResponse(BaseModel):
    user_id: str
    display_name: str = ""
    avatar_url: str = ""
    email: str = ""


# ====================== TEAM ======================

class TeamCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=100)
    description: str = ""


class TeamUpdate(BaseModel):
    name: Optional[str] = Field(default=None, max_length=100)
    description: Optional[str] = None


class TeamMemberResponse(BaseModel):
    user_id: str
    role: str  # "owner" | "member"
    joined_at: Optional[str] = None
    display_name: str = ""
    avatar_url: str = ""


class TeamResponse(BaseModel):
    id: str
    name: str
    description: str = ""
    owner_id: str
    invite_code: str
    members: List[TeamMemberResponse] = []
    member_count: int = 0
    created_at: Optional[str] = None


# ====================== TEAM MEMO ======================

class TeamMemoCreate(BaseModel):
    content: str
    tags: List[str] = []


class TeamMemoResponse(BaseModel):
    id: str
    content: str
    tags: List[str] = []
    creator_id: str
    creator_name: str = ""
    creator_avatar: str = ""
    team_id: str
    space: str = "draft"  # "draft" | "main"
    status: str = "draft"  # "draft" | "pending_review" | "approved" | "merged"
    pinned: bool = False
    merged_at: Optional[str] = None
    merged_by: Optional[str] = None
    merged_by_name: str = ""
    comment_count: int = 0
    reaction_counts: dict = {}  # {"👍": 2, "💡": 1}
    user_reactions: List[str] = []  # reactions by current user
    created_at: Optional[str] = None
    updated_at: Optional[str] = None


# ====================== COMMENTS ======================

class TeamCommentCreate(BaseModel):
    content: str = Field(..., min_length=1, max_length=2000)


class TeamCommentResponse(BaseModel):
    id: str
    memo_id: str
    user_id: str
    user_name: str = ""
    user_avatar: str = ""
    content: str
    created_at: Optional[str] = None


# ====================== REVIEW ======================

class ReviewAction(BaseModel):
    action: str = Field(..., pattern="^(approve|reject)$")
    reason: str = ""


# ====================== REACTIONS ======================

class ReactionToggle(BaseModel):
    emoji: str = Field(..., min_length=1, max_length=4)
