from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional, TypedDict


# ---------------------------------------------------------------------------
# Literal type aliases
# ---------------------------------------------------------------------------

Platform = Literal[
    "twitter",
    "linkedin",
    "facebook",
    "instagram",
    "tiktok",
    "bluesky",
    "pinterest",
    "threads",
    "youtube",
]

PostStatus = Literal["draft", "scheduled", "posted", "failed"]

MediaType = Literal["image", "video", "thumbnail"]


# ---------------------------------------------------------------------------
# Core resource types
# ---------------------------------------------------------------------------

class Post(TypedDict, total=False):
    """A social media post."""
    id: str
    content: Any
    platform: str
    status: str
    scheduled_for: Optional[str]
    posted_at: Optional[str]
    created_at: str


class Pagination(TypedDict, total=False):
    """Pagination metadata returned with list endpoints."""
    total: int
    limit: int
    offset: int


# ---------------------------------------------------------------------------
# Post parameter types
# ---------------------------------------------------------------------------

class CreatePostParams(TypedDict, total=False):
    """Parameters for creating a single post."""
    content: str
    platform: str
    scheduled_for: str
    media: List[str]
    platform_settings: Dict[str, Any]
    thread: List[str]
    thread_media: Dict[str, List[str]]
    thread_delay: int


class UpdatePostParams(TypedDict, total=False):
    """Parameters for updating an existing post."""
    content: str
    platform: str
    scheduled_for: str
    media: List[str]
    platform_settings: Dict[str, Any]


class BatchPostParams(TypedDict, total=False):
    """Parameters for creating posts across multiple platforms in a single batch."""
    content: str
    platforms: List[str]
    scheduled_for: str
    platform_content: Dict[str, str]
    media: List[str]
    platform_settings: Dict[str, Dict[str, Any]]


class ListPostsParams(TypedDict, total=False):
    """Query parameters for listing posts."""
    status: str
    platform: str
    sort: str
    from_date: str  # maps to 'from' query param
    to_date: str    # maps to 'to' query param
    limit: int
    offset: int


# ---------------------------------------------------------------------------
# Analytics types
# ---------------------------------------------------------------------------

class AnalyticsParams(TypedDict, total=False):
    """Parameters for retrieving analytics data."""
    platform: str
    period: str
    post_id: str


class PlatformAnalytics(TypedDict, total=False):
    """Per-platform analytics breakdown."""
    platform: str
    posts_published: int


class AnalyticsData(TypedDict, total=False):
    """Analytics data returned from the analytics endpoint."""
    period: str
    since: str
    total_posts: int
    total_platform_posts: int
    by_platform: List[PlatformAnalytics]


# ---------------------------------------------------------------------------
# Account types
# ---------------------------------------------------------------------------

class Account(TypedDict, total=False):
    """A connected social media account."""
    id: str
    provider: str
    name: str
    status: str
    connected_at: str


class PlatformCapabilities(TypedDict, total=False):
    """Platform capabilities describing what a platform supports."""
    character_limit: int
    supports_thread: bool
    supports_media: bool
    max_media_images: int
    max_media_videos: int
    available_settings: Dict[str, str]


class AccountSettings(TypedDict, total=False):
    """Settings and capabilities for a connected account."""
    account_id: str
    platform: str
    capabilities: PlatformCapabilities
    defaults: Dict[str, Any]
    available_tools: List[str]


class TriggerToolParams(TypedDict, total=False):
    """Parameters for triggering a platform tool on an account."""
    tool: str


class TriggerToolResult(TypedDict, total=False):
    """Result from triggering a platform tool."""
    tool: str
    platform: str
    results: List[Any]


# ---------------------------------------------------------------------------
# AI types
# ---------------------------------------------------------------------------

class GenerateParams(TypedDict, total=False):
    """Parameters for AI content generation."""
    prompt: str
    platform: str
    tone: str


class GenerateResult(TypedDict, total=False):
    """Result from AI content generation."""
    content: str
    platform: str
    tone: str


# ---------------------------------------------------------------------------
# Workspace types
# ---------------------------------------------------------------------------

class WorkspaceLimits(TypedDict, total=False):
    """Plan limits for a workspace."""
    api_requests_per_hour: int
    api_keys_max: int
    max_posts_per_month: int
    max_ai_credits_per_month: int


class WorkspaceUsage(TypedDict, total=False):
    """Current usage stats for a workspace."""
    posts_this_month: int


class Workspace(TypedDict, total=False):
    """Workspace information including plan and usage limits."""
    id: str
    name: str
    created_at: str
    plan: str
    limits: WorkspaceLimits
    usage: WorkspaceUsage


# ---------------------------------------------------------------------------
# Media types
# ---------------------------------------------------------------------------

class MediaUpload(TypedDict, total=False):
    """A successfully uploaded media file."""
    media_id: str
    key: str
    type: str
    url: str
    content_type: str
    size: int


class UploadFromUrlParams(TypedDict, total=False):
    """Parameters for uploading media from a remote URL."""
    url: str
    type: str


# ---------------------------------------------------------------------------
# Webhook types
# ---------------------------------------------------------------------------

class Webhook(TypedDict, total=False):
    """A registered webhook."""
    id: str
    url: str
    events: List[str]
    is_active: bool
    created_at: str


class WebhookCreated(TypedDict, total=False):
    """A newly created webhook, includes the signing secret."""
    id: str
    url: str
    events: List[str]
    secret: str
    is_active: bool
    created_at: str


class CreateWebhookParams(TypedDict, total=False):
    """Parameters for creating a new webhook."""
    url: str
    events: List[str]
