"""
Direct PDF Link Downloader
"""

from typing import Dict, Optional
from urllib.parse import urlparse
from loguru import logger

from .base import BaseDownloader, ContentType, DownloadResult


class DirectPDFDownloader(BaseDownloader):
    """Downloader for direct PDF links."""

    def can_handle(self, url: str) -> bool:
        """Check if URL is a direct PDF link using proper URL parsing."""
        try:
            # Parse the URL
            parsed = urlparse(url.lower())
            path = parsed.path or ""
            query = parsed.query or ""

            # Check for .pdf extension in path
            if path.endswith(".pdf"):
                return True

            # Check for .pdf with query parameters
            if ".pdf?" in url.lower():
                return True

            # Check for /pdf/ in path
            if "/pdf/" in path:
                return True

            # Check query parameters for PDF format
            if "type=pdf" in query or "format=pdf" in query:
                return True

            return False

        except Exception:
            logger.warning(f"Error parsing URL {url}")
            return False

    def download(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> Optional[bytes]:
        """Download PDF directly from URL."""
        if content_type == ContentType.TEXT:
            # Download PDF and extract text
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return text.encode("utf-8")
            return None
        return self._download_pdf(url)

    def download_with_result(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> DownloadResult:
        """Download content and return detailed result with skip reason."""
        if content_type == ContentType.TEXT:
            # Download PDF and extract text
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return DownloadResult(
                        content=text.encode("utf-8"), is_success=True
                    )
                return DownloadResult(
                    skip_reason="PDF downloaded but text extraction failed"
                )
            return DownloadResult(
                skip_reason="Could not download PDF from direct link"
            )
        # Try to download PDF directly
        logger.info(f"Attempting direct PDF download from {url}")
        pdf_content = super()._download_pdf(url)

        if pdf_content:
            logger.info(f"Successfully downloaded PDF directly from {url}")
            return DownloadResult(content=pdf_content, is_success=True)
        # Try to determine specific reason for failure
        try:
            response = self.session.head(url, timeout=5, allow_redirects=True)
            if response.status_code == 404:
                return DownloadResult(
                    skip_reason="PDF file not found (404) - link may be broken",
                    status_code=404,
                )
            if response.status_code == 403:
                return DownloadResult(
                    skip_reason="Access denied (403) - PDF requires authentication",
                    status_code=403,
                )
            if response.status_code >= 500:
                return DownloadResult(
                    skip_reason=f"Server error ({response.status_code}) - try again later",
                    status_code=response.status_code,
                )
            return DownloadResult(
                skip_reason=f"Could not download PDF - server returned status {response.status_code}",
                status_code=response.status_code,
            )
        except Exception:
            return DownloadResult(
                skip_reason="Failed to download PDF from direct link"
            )

    def _download_pdf(
        self, url: str, headers: Optional[Dict[str, str]] = None
    ) -> Optional[bytes]:
        """Download PDF directly from URL."""
        logger.info(f"Downloading PDF directly from: {url}")
        return super()._download_pdf(url)
