"""Type definitions for the Resletter SDK."""

from typing import Any, Literal, TypedDict  # noqa: I001


# ==================== COMMON TYPES ====================


class Pagination(TypedDict):
    """Pagination information for list responses."""

    page: int
    limit: int
    total: int
    pages: int


# ==================== SUBSCRIBER TYPES ====================

SubscriberStatus = Literal["subscribed", "pending", "unsubscribed", "bounced"]


class GroupRef(TypedDict):
    """Reference to a subscriber group."""

    id: str
    name: str


class Subscriber(TypedDict, total=False):
    """Subscriber data returned from the API."""

    id: str
    email: str
    name: str | None
    status: SubscriberStatus
    metadata: dict[str, Any]
    groups: list[GroupRef]
    subscribedAt: str | None
    createdAt: str
    updatedAt: str


class ListSubscribersParams(TypedDict, total=False):
    """Parameters for listing subscribers."""

    page: int
    limit: int
    status: SubscriberStatus
    groupId: str
    email: str


class ListSubscribersResponse(TypedDict):
    """Response from listing subscribers."""

    subscribers: list[Subscriber]
    pagination: Pagination


class CreateSubscriberParams(TypedDict, total=False):
    """Parameters for creating a subscriber."""

    email: str  # Required
    name: str
    metadata: dict[str, Any]
    groupIds: list[str]
    bypassDoubleOptIn: bool


class CreateSubscriberResponse(TypedDict):
    """Response from creating a subscriber."""

    id: str
    email: str
    name: str | None
    status: SubscriberStatus
    isNew: bool


class BulkSubscriberData(TypedDict, total=False):
    """Individual subscriber data for bulk creation."""

    email: str  # Required
    name: str
    metadata: dict[str, Any]


class CreateBulkSubscribersParams(TypedDict, total=False):
    """Parameters for bulk subscriber creation."""

    subscribers: list[BulkSubscriberData]  # Required
    groupIds: list[str]
    skipDuplicates: bool
    bypassDoubleOptIn: bool


class BulkCreateResults(TypedDict):
    """Results from bulk subscriber creation."""

    created: int
    skipped: int
    pending: int
    errors: list[str]


class CreateBulkSubscribersResponse(TypedDict):
    """Response from bulk subscriber creation."""

    message: str
    results: BulkCreateResults
    bypassedDoubleOptIn: bool


class UpdateSubscriberParams(TypedDict, total=False):
    """Parameters for updating a subscriber."""

    name: str
    metadata: dict[str, Any]
    groupIds: list[str]


# ==================== GROUP TYPES ====================


class SubscriberGroup(TypedDict, total=False):
    """Subscriber group data returned from the API."""

    id: str
    name: str
    description: str | None
    color: str | None
    subscriberCount: int
    createdAt: str
    updatedAt: str


class CreateGroupParams(TypedDict, total=False):
    """Parameters for creating a group."""

    name: str  # Required
    description: str
    color: str


class UpdateGroupParams(TypedDict, total=False):
    """Parameters for updating a group."""

    name: str
    description: str
    color: str


class AddSubscribersToGroupParams(TypedDict):
    """Parameters for adding subscribers to a group."""

    subscriberIds: list[str]


class RemoveSubscribersFromGroupParams(TypedDict):
    """Parameters for removing subscribers from a group."""

    subscriberIds: list[str]


class AddSubscribersToGroupResponse(TypedDict):
    """Response from adding subscribers to a group."""

    message: str
    added: int
    alreadyInGroup: int


class RemoveSubscribersFromGroupResponse(TypedDict):
    """Response from removing subscribers from a group."""

    message: str
    removed: int


# ==================== CAMPAIGN TYPES ====================

CampaignStatus = Literal["active", "paused", "completed", "archived"]


class Campaign(TypedDict, total=False):
    """Campaign data returned from the API."""

    id: str
    name: str
    description: str | None
    status: CampaignStatus
    subscriberGroupIds: list[str]
    newsletterCount: int
    createdAt: str
    updatedAt: str


class ListCampaignsParams(TypedDict, total=False):
    """Parameters for listing campaigns."""

    status: CampaignStatus


class CreateCampaignParams(TypedDict, total=False):
    """Parameters for creating a campaign."""

    name: str  # Required
    description: str
    subscriberGroupIds: list[str]


class UpdateCampaignParams(TypedDict, total=False):
    """Parameters for updating a campaign."""

    name: str
    description: str
    status: CampaignStatus
    subscriberGroupIds: list[str]


# ==================== EVENT TYPES ====================


class TrackEventParams(TypedDict, total=False):
    """Parameters for tracking an event."""

    name: str  # Required
    subscriberId: str
    email: str
    data: dict[str, Any]


class TrackEventResponse(TypedDict):
    """Response from tracking an event."""

    eventName: str
    subscriberId: str
    automationsTriggered: int
    automationIds: list[str]


# ==================== INTERNAL API RESPONSE TYPES ====================


class ApiResponse(TypedDict):
    """Standard API response wrapper."""

    success: bool
    data: Any
    message: str


class ApiErrorDetail(TypedDict, total=False):
    """API error detail."""

    message: str
    code: str


class ApiErrorResponse(TypedDict):
    """API error response."""

    success: Literal[False]
    error: ApiErrorDetail
