from typing import Optional
from bs4 import BeautifulSoup
from .base import BaseVideoPlayer
import re


class SendVidDownloader(BaseVideoPlayer):
    """Downloader for SendVid videos"""

    def can_handle(self, url: str) -> bool:
        return "sendvid.com" in url.lower()

    async def _fetch_page(self, url: str) -> str:
        """Fetch page with proper headers to avoid 403 errors"""
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
            'Referer': 'https://sendvid.com/',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
        }
        response = await self.client.get(url, headers=headers)
        response.raise_for_status()
        return response.text

    async def get_download_link(self, url: str, target_filename: str = None) -> tuple[str, str]:
        """
        Extract direct download link from SendVid embed page
        SendVid embed pages contain the direct MP4 URL in a <source> tag
        """
        print(f"[SENDVID] Fetching page: {url}")

        html = await self._fetch_page(url)
        soup = BeautifulSoup(html, 'lxml')

        # Try to find the video source in the <source> tag
        source_tag = soup.find('source', {'id': 'video_source'})
        if source_tag and source_tag.get('src'):
            video_url = source_tag['src']
            print(f"[SENDVID] Found video URL in <source> tag")

            # Generate filename
            if target_filename:
                filename = target_filename
            else:
                # Extract filename from video URL or generate one
                filename = self._extract_filename_from_url(url, video_url)

            print(f"[SENDVID] Download URL: {video_url}")
            print(f"[SENDVID] Filename: {filename}")
            return video_url, filename

        # Fallback: try to find in og:video meta property
        og_video = soup.find('meta', {'property': 'og:video'})
        if og_video and og_video.get('content'):
            video_url = og_video['content']
            print(f"[SENDVID] Found video URL in og:video meta")

            if target_filename:
                filename = target_filename
            else:
                filename = self._extract_filename_from_url(url, video_url)

            print(f"[SENDVID] Download URL: {video_url}")
            print(f"[SENDVID] Filename: {filename}")
            return video_url, filename

        raise Exception("Could not extract video URL from SendVid page")

    def _extract_filename_from_url(self, page_url: str, video_url: str) -> str:
        """Generate filename from SendVod URLs"""
        # Try to extract video ID from page URL
        video_id_match = re.search(r'/embed/([a-z0-9]+)', page_url)
        if video_id_match:
            video_id = video_id_match.group(1)
            # Try to get title from page (might need to fetch, but for now use ID)
            return f"sendvid_{video_id}.mp4"

        # Fallback: extract from video URL
        filename_match = re.search(r'/([^/]+\.mp4)', video_url)
        if filename_match:
            return filename_match.group(1)

        return "sendvid_video.mp4"
