"""Base class for video hosting services (players)"""
from abc import abstractmethod
from typing import Optional, Tuple
import logging
import httpx
from bs4 import BeautifulSoup

logger = logging.getLogger(__name__)


class BaseVideoPlayer:
    """
    Base class for video hosting services.

    Video players host actual video files and provide direct download links.
    They extract URLs from embedded players and handle file downloads.

    Examples: Doodstream, Sibnet, VidMoly, SendVid, Lpayer, 1fichier, etc.

    KEY FEATURE: Flexible get_download_link() signature to support:
    - Standard: get_download_link(url)
    - With target_filename: get_download_link(url, target_filename="...")  (VidMoly, SendVid)
    """

    def __init__(self):
        # Realistic browser headers to avoid blocking by video hosts
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
            "Accept-Language": "en-US,en;q=0.9,fr;q=0.8",
            "Referer": "https://anime-sama.tv/",
        }
        # Initialize HTTP client with browser headers
        self.client = httpx.AsyncClient(timeout=10.0, follow_redirects=True, headers=headers)

    @abstractmethod
    def can_handle(self, url: str) -> bool:
        """Check if this player can handle the given URL"""
        pass

    @abstractmethod
    async def get_download_link(
        self,
        url: str,
        target_filename: Optional[str] = None
    ) -> Tuple[str, str]:
        """
        Extract direct download link and filename from video player URL.

        Args:
            url: The video player URL
            target_filename: Optional filename override (used by VidMoly, SendVid)

        Returns:
            Tuple of (download_url, filename)

        Note:
            - Always use sanitize_filename() on extracted filenames!
            - target_filename parameter is optional but MUST be supported
              for compatibility with VidMoly and SendVid
        """
        pass

    # Common methods for all video players
    async def close(self):
        """Close HTTP client"""
        await self.client.aclose()

    async def _fetch_page(self, url: str) -> str:
        """Fetch HTML page content"""
        response = await self.client.get(url)
        response.raise_for_status()
        return response.text

    def _parse_html(self, html: str) -> BeautifulSoup:
        """Parse HTML with BeautifulSoup"""
        return BeautifulSoup(html, 'lxml')

    def _extract_filename_from_headers(self, headers: dict) -> Optional[str]:
        """Extract filename from Content-Disposition header"""
        from app.utils import sanitize_filename

        content_disposition = headers.get("content-disposition", "")
        if "filename=" in content_disposition:
            filename = content_disposition.split("filename=")[-1].strip('"')
            return sanitize_filename(filename)  # Security!
        return None

    def _sanitize(self, filename: str) -> str:
        """Convenience method for filename sanitization"""
        from app.utils import sanitize_filename
        return sanitize_filename(filename)
