"""
Pydantic schemas for Reddit posts and comments.

This module defines request and response schemas for Reddit-related operations.
"""

from datetime import datetime
from typing import Optional
from typing import List

from pydantic import BaseModel, Field, ConfigDict


class RedditPostBase(BaseModel):
    """Base schema for Reddit post data."""
    post_id: str = Field(..., description="Unique identifier from Reddit")
    title: str = Field(..., min_length=1, max_length=500, description="Post title")
    text: Optional[str] = Field(None, description="Post content")
    upvotes: int = Field(default=0, ge=0, description="Number of upvotes")
    created_at: datetime = Field(..., description="Timestamp when post was created")
    match_id: Optional[int] = Field(None, description="Foreign key to matches table")
    subreddit: str = Field(..., description="Subreddit name")
    source: str = Field(default="reddit", description="Source platform")


class RedditPostCreate(RedditPostBase):
    """Schema for creating a new Reddit post."""
    pass


class RedditPostResponse(RedditPostBase):
    """Schema for Reddit post response."""
    id: int = Field(..., description="Primary key")
    model_config = ConfigDict(from_attributes=True)


class RedditCommentBase(BaseModel):
    """Base schema for Reddit comment data."""
    comment_id: str = Field(..., description="Unique identifier from Reddit")
    post_id: str = Field(..., description="Foreign key to posts_reddit table")
    text: str = Field(..., min_length=1, description="Comment content")
    upvotes: int = Field(default=0, ge=0, description="Number of upvotes")
    created_at: datetime = Field(..., description="Timestamp when comment was created")
    source: str = Field(default="reddit", description="Source platform")


class RedditCommentCreate(RedditCommentBase):
    """Schema for creating a new Reddit comment."""
    pass


class RedditCommentResponse(RedditCommentBase):
    """Schema for Reddit comment response."""
    id: int = Field(..., description="Primary key")
    model_config = ConfigDict(from_attributes=True)


class RedditPostListResponse(BaseModel):
    """Schema for a list of Reddit posts."""
    data: list[RedditPostResponse]
    count: int = Field(..., description="Total number of posts")
    meta: dict = Field(default_factory=dict, description="Additional metadata")


class RedditCommentListResponse(BaseModel):
    """Schema for a list of Reddit comments."""
    data: list[RedditCommentResponse]
    count: int = Field(..., description="Total number of comments")
    meta: dict = Field(default_factory=dict, description="Additional metadata")


class RedditStatsResponse(BaseModel):
    """Schema for Reddit statistics."""
    total_posts: int = Field(..., description="Total number of Reddit posts")
    total_comments: int = Field(..., description="Total number of Reddit comments")
    posts_by_subreddit: dict = Field(..., description="Breakdown by subreddit")
    posts_by_match: dict = Field(..., description="Breakdown by match")
    avg_post_upvotes: float = Field(..., description="Average post upvotes")
    avg_comment_upvotes: float = Field(..., description="Average comment upvotes")
