"""Uqload video hosting service downloader"""
import logging
import re
from typing import Optional
from .base import BaseVideoPlayer
from bs4 import BeautifulSoup
from app.utils import sanitize_filename

logger = logging.getLogger(__name__)


class UqloadDownloader(BaseVideoPlayer):
    """
    Downloader for Uqload video hosting service.

    Uqload is a video hosting platform used by French Stream and other streaming sites.
    """

    def can_handle(self, url: str) -> bool:
        """Check if this downloader can handle the given URL"""
        return "uqload" in url.lower()

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

        Args:
            url: The Uqload video player URL
            target_filename: Optional filename override

        Returns:
            Tuple of (download_url, filename)
        """
        try:
            logger.info(f"Fetching Uqload URL: {url}")

            # Fetch the page
            response = await self.client.get(url)
            response.raise_for_status()
            html = response.text

            # Method 1: Look for video URL in JavaScript
            # Uqload stores the video URL in a JavaScript variable like: sources: ["URL"]
            patterns = [
                r'sources:\s*\["([^"]+\.mp4[^"]*)"\]',
                r'sources:\s*\[["\']([^"\']+\.mp4[^"\']*)["\']\]',
                r'"sources":\s*\["([^"]+\.mp4[^"]*)"\]',
                r'file:\s*"([^"]+\.mp4[^"]*)"',
                r'file:\s*["\']([^"\']+\.mp4[^"\']*)["\']',
                r'"file"\s*:\s*"([^"]+\.mp4[^"]*)"',
            ]

            for pattern in patterns:
                match = re.search(pattern, html)
                if match:
                    download_url = match.group(1)
                    # Clean up any escape characters
                    download_url = download_url.replace('\\/', '/')
                    logger.info(f"Found video source from JavaScript pattern: {pattern[:20]}...")
                    break
            else:
                # Method 2: Try parsing with BeautifulSoup
                soup = BeautifulSoup(html, 'lxml')

                # Look for video tag
                video_tag = soup.find('video')
                if video_tag and video_tag.get('src'):
                    download_url = video_tag['src']
                    logger.info(f"Found video source from <video> tag")
                else:
                    # Look for source tag
                    source_tag = soup.find('source')
                    if source_tag and source_tag.get('src'):
                        download_url = source_tag['src']
                        logger.info(f"Found video source from <source> tag")
                    else:
                        raise ValueError("Could not find video URL in Uqload page")

            # Ensure URL is absolute
            if not download_url.startswith('http'):
                if download_url.startswith('//'):
                    download_url = 'https:' + download_url
                else:
                    from urllib.parse import urljoin
                    download_url = urljoin(url, download_url)

            # Generate filename
            if target_filename:
                filename = sanitize_filename(target_filename)
            else:
                # Try to extract filename from URL
                filename = download_url.split('/')[-1].split('?')[0]
                if not filename or len(filename) < 5:
                    filename = "uqload_video.mp4"
                filename = sanitize_filename(filename)

            # Ensure .mp4 extension
            if not filename.endswith('.mp4'):
                filename += '.mp4'

            logger.info(f"Successfully extracted Uqload download link: {filename}")
            return download_url, filename

        except Exception as e:
            logger.error(f"Error extracting Uqload download link: {e}")
            raise ValueError(f"Failed to extract download link from Uqload: {str(e)}")
