"""
Tools resource for media download and utilities.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ..client.base import BaseClient


class ToolsResource:
    """
    Resource for media download and utility tools.

    Rate limits: Build (50/day), Accelerate (500/day), Unlimited (unlimited)
    """

    _BASE_PATH = "/v1/tools"

    def __init__(self, client: "BaseClient") -> None:
        self._client = client

    # YouTube
    def youtube_download(self, url: str, format_id: str | None = None) -> dict[str, Any]:
        """Download YouTube video or audio."""
        params: dict[str, Any] = {"url": url}
        if format_id:
            params["formatId"] = format_id
        return self._client._get(f"{self._BASE_PATH}/youtube/download", params=params)

    def youtube_transcript(self, url: str, lang: str | None = None) -> dict[str, Any]:
        """Get YouTube video transcript."""
        params: dict[str, Any] = {"url": url}
        if lang:
            params["lang"] = lang
        return self._client._get(f"{self._BASE_PATH}/youtube/transcript", params=params)

    # Instagram
    def instagram_download(self, url: str) -> dict[str, Any]:
        """Download Instagram reel or post."""
        return self._client._get(f"{self._BASE_PATH}/instagram/download", params={"url": url})

    def instagram_hashtag_check(self, hashtags: list[str]) -> dict[str, Any]:
        """Check Instagram hashtags for bans."""
        return self._client._post(
            f"{self._BASE_PATH}/instagram/hashtag-checker",
            data={"hashtags": hashtags},
        )

    # TikTok
    def tiktok_download(self, url: str, no_watermark: bool = True) -> dict[str, Any]:
        """Download TikTok video."""
        return self._client._get(
            f"{self._BASE_PATH}/tiktok/download",
            params={"url": url, "noWatermark": str(no_watermark).lower()},
        )

    # Twitter
    def twitter_download(self, url: str) -> dict[str, Any]:
        """Download Twitter/X video."""
        return self._client._get(f"{self._BASE_PATH}/twitter/download", params={"url": url})

    # Facebook
    def facebook_download(self, url: str) -> dict[str, Any]:
        """Download Facebook video."""
        return self._client._get(f"{self._BASE_PATH}/facebook/download", params={"url": url})

    # LinkedIn
    def linkedin_download(self, url: str) -> dict[str, Any]:
        """Download LinkedIn video."""
        return self._client._get(f"{self._BASE_PATH}/linkedin/download", params={"url": url})

    # Bluesky
    def bluesky_download(self, url: str) -> dict[str, Any]:
        """Download Bluesky video."""
        return self._client._get(f"{self._BASE_PATH}/bluesky/download", params={"url": url})

    # AI Caption Generator
    def generate_caption(
        self,
        image_url: str,
        *,
        prompt: str | None = None,
        tone: str | None = None,
    ) -> dict[str, Any]:
        """Generate AI captions for an image."""
        payload: dict[str, Any] = {"imageUrl": image_url}
        if prompt:
            payload["prompt"] = prompt
        if tone:
            payload["tone"] = tone
        return self._client._post(f"{self._BASE_PATH}/caption-generator", data=payload)

    # Async versions
    async def ayoutube_download(self, url: str, format_id: str | None = None) -> dict[str, Any]:
        params: dict[str, Any] = {"url": url}
        if format_id:
            params["formatId"] = format_id
        return await self._client._aget(f"{self._BASE_PATH}/youtube/download", params=params)

    async def agenerate_caption(self, image_url: str, **kwargs: Any) -> dict[str, Any]:
        payload: dict[str, Any] = {"imageUrl": image_url, **kwargs}
        return await self._client._apost(f"{self._BASE_PATH}/caption-generator", data=payload)
