"""Async Links service - URL shortening."""

from typing import Any, Dict, Optional
from sed_sh.services.async_base import AsyncBaseService


class AsyncLinksService(AsyncBaseService):
    """Async service for managing shortened URLs.

    All methods may raise SedSHError or its subclasses on error.
    No automatic retries are performed - implement retry logic in your application if needed.
    No client-side validation is performed - validation errors come from the API.
    """

    async def create(
        self, target_url: str, password: Optional[str] = None
    ) -> Dict[str, Any]:
        """Create a shortened URL asynchronously.

        Args:
            target_url: The URL to shorten
            password: Optional password protection for the link

        Returns:
            Dict containing link data (see sync version for details)

        Raises:
            InvalidRequestError: If target_url is invalid
            AuthenticationError: If API key is invalid
            RateLimitError: If rate limit is exceeded
            SedSHError: On other errors
        """
        payload: Dict[str, Any] = {"targetUrl": target_url}
        if password is not None:
            payload["password"] = password

        return await self._request("POST", "/links", json=payload)

    async def list(self) -> Dict[str, Any]:
        """List all links created with your API key asynchronously.

        Returns:
            Dict containing links list (see sync version for details)

        Raises:
            AuthenticationError: If API key is invalid
            SedSHError: On other errors
        """
        return await self._request("GET", "/links")

    async def delete(self, code: str) -> Dict[str, Any]:
        """Delete a shortened link asynchronously.

        Args:
            code: The short code of the link to delete

        Returns:
            Dict containing success confirmation

        Raises:
            ResourceNotFoundError: If link code doesn't exist
            AuthenticationError: If API key is invalid
            SedSHError: On other errors
        """
        return await self._request("DELETE", f"/links/{code}")
