"""Descarga una pagina web y la convierte a markdown."""

from __future__ import annotations


def fetch_and_parse_url(url: str, timeout: float = 30.0) -> str:
    """Descarga una pagina web y la convierte a markdown.

    Detecta el tipo de URL con detect_url_type, descarga el contenido con
    httpx y lo convierte al formato apropiado:
    - webpage: fetch HTML → html_to_markdown
    - markdown: retorna el texto directamente
    - text/code: retorna el texto directamente
    - pdf: retorna stub (requiere dependencia externa)
    - code_repository: retorna stub (requiere clonar repo)

    Args:
        url: URL a descargar y parsear.
        timeout: Timeout en segundos para las peticiones HTTP.

    Returns:
        Contenido de la URL en formato markdown.

    Raises:
        Exception: Si falla la descarga (timeout, DNS, HTTP error) o el tipo
                   de URL no es soportado.
    """
    import httpx

    from detect_url_type import detect_url_type
    from html_to_markdown import html_to_markdown

    # Detectar tipo de URL (puede hacer HEAD request)
    url_type, _meta = detect_url_type(url, timeout=timeout)

    if url_type == "code_repository":
        raise Exception(
            f"fetch_and_parse_url: code_repository URLs require git clone, not supported. url={url!r}"
        )

    if url_type == "pdf":
        raise Exception(
            f"fetch_and_parse_url: PDF parsing requires external dependency (pdfminer/pypdf). url={url!r}"
        )

    # Fetch content via GET
    try:
        response = httpx.get(url, timeout=timeout, follow_redirects=True)
        response.raise_for_status()
    except httpx.HTTPStatusError as exc:
        raise Exception(
            f"fetch_and_parse_url: HTTP {exc.response.status_code} for {url!r}"
        ) from exc
    except Exception as exc:
        raise Exception(f"fetch_and_parse_url: request failed for {url!r}: {exc}") from exc

    content_type = response.headers.get("content-type", "").lower()
    raw_text = response.text

    if url_type == "webpage" or "text/html" in content_type:
        return html_to_markdown(raw_text)

    # markdown, text, or code files — return as-is
    return raw_text
