"""Type definitions for Short Links endpoint."""

from datetime import datetime
from typing import Optional

from openai import BaseModel as OpenAIBaseModel
from pydantic import BaseModel, Field


class ShortLinkCreateRequest(BaseModel):
    """Request to create a short link."""

    url: str
    """Target URL to shorten."""

    custom_slug: Optional[str] = Field(
        None,
        max_length=50,
        pattern=r"^[a-z0-9-]+$",
        description="Custom slug (lowercase alphanumeric and hyphens)",
    )
    """Optional custom slug for the short link."""

    max_hits: Optional[int] = Field(None, ge=1)
    """Maximum number of hits before link expires."""

    ttl: Optional[str] = None
    """Time to live (e.g., '7d', '24h', '30m')."""


class ShortLink(OpenAIBaseModel):
    """Short link object."""

    code: str
    """Unique short link code."""

    short_url: str
    """Full short URL."""

    target_url: str
    """Target URL the link redirects to."""

    custom_slug: Optional[str] = None
    """Custom slug if set."""

    max_hits: Optional[int] = None
    """Maximum number of hits."""

    hits: int
    """Current number of hits."""

    expires_at: Optional[datetime] = None
    """Expiration datetime."""

    is_active: bool
    """Whether the link is active."""

    created_at: datetime
    """Creation datetime."""


class ShortLinkCreateResponse(OpenAIBaseModel):
    """Response from creating a short link."""

    code: str
    """Unique short link code."""

    short_url: str
    """Full short URL."""

    target_url: str
    """Target URL the link redirects to."""


class ShortLinkListResponse(OpenAIBaseModel):
    """Response from listing short links."""

    data: list[ShortLink]
    """List of short links."""

    total: int
    """Total number of links."""

    limit: int
    """Page limit."""

    offset: int
    """Page offset."""


class ShortLinkStatsResponse(OpenAIBaseModel):
    """Statistics response for short links."""

    total_links: int
    """Total number of links."""

    total_hits: int
    """Total number of hits across all links."""

    active_links: int
    """Number of active links."""


class ShortLinkDeleteResponse(OpenAIBaseModel):
    """Response from deactivating a short link."""

    deleted: bool
    """Whether the link was deactivated."""

    code: str
    """Code of the deactivated link."""
