"""
Legal Corpus API - Background Ingestion Tasks

Background tasks for ingesting uploaded documents and URL links
into the legal corpus vector database.

Extracted from legal_corpus_routes.py to keep files under 500 LOC.
"""

import os
import logging
from typing import Dict, Any, Optional, List

import httpx

logger = logging.getLogger(__name__)


async def ingest_uploaded_document(doc_record: Dict[str, Any]):
    """Background task to ingest an uploaded document."""
    try:
        doc_record["status"] = "processing"

        from legal_corpus_ingestion import LegalCorpusIngestion
        ingestion = LegalCorpusIngestion()

        import fitz
        doc = fitz.open(doc_record["file_path"])
        full_text = ""
        for page in doc:
            full_text += page.get_text()
        doc.close()

        if not full_text.strip():
            doc_record["status"] = "error"
            doc_record["error"] = "No text could be extracted from PDF"
            return

        chunks = ingestion.chunk_text(full_text, doc_record["code"])

        for chunk in chunks:
            chunk["regulation_code"] = doc_record["code"]
            chunk["regulation_name"] = doc_record["title"]
            chunk["document_type"] = doc_record["document_type"]
            chunk["source_url"] = f"upload://{doc_record['filename']}"

        if chunks:
            await ingestion.embed_and_upsert(chunks)
            doc_record["chunk_count"] = len(chunks)
            doc_record["status"] = "indexed"
            logger.info(f"Ingested {len(chunks)} chunks from uploaded document {doc_record['code']}")
        else:
            doc_record["status"] = "error"
            doc_record["error"] = "No chunks generated from document"

    except Exception as e:
        logger.error(f"Failed to ingest uploaded document: {e}")
        doc_record["status"] = "error"
        doc_record["error"] = str(e)


async def ingest_link_document(doc_record: Dict[str, Any]):
    """Background task to ingest content from a URL."""
    try:
        doc_record["status"] = "fetching"

        async with httpx.AsyncClient(timeout=60.0) as client:
            response = await client.get(doc_record["url"], follow_redirects=True)
            response.raise_for_status()

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

            if "application/pdf" in content_type:
                import tempfile
                with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
                    f.write(response.content)
                    pdf_path = f.name

                import fitz
                pdf_doc = fitz.open(pdf_path)
                full_text = ""
                for page in pdf_doc:
                    full_text += page.get_text()
                pdf_doc.close()
                os.unlink(pdf_path)

            elif "text/html" in content_type:
                from bs4 import BeautifulSoup
                soup = BeautifulSoup(response.text, "html.parser")

                for script in soup(["script", "style", "nav", "footer", "header"]):
                    script.decompose()

                full_text = soup.get_text(separator="\n", strip=True)

            else:
                full_text = response.text

        if not full_text.strip():
            doc_record["status"] = "error"
            doc_record["error"] = "No text could be extracted from URL"
            return

        doc_record["status"] = "processing"

        from legal_corpus_ingestion import LegalCorpusIngestion
        ingestion = LegalCorpusIngestion()

        chunks = ingestion.chunk_text(full_text, doc_record["code"])

        for chunk in chunks:
            chunk["regulation_code"] = doc_record["code"]
            chunk["regulation_name"] = doc_record["title"]
            chunk["document_type"] = doc_record["document_type"]
            chunk["source_url"] = doc_record["url"]

        if chunks:
            await ingestion.embed_and_upsert(chunks)
            doc_record["chunk_count"] = len(chunks)
            doc_record["status"] = "indexed"
            logger.info(f"Ingested {len(chunks)} chunks from URL {doc_record['url']}")
        else:
            doc_record["status"] = "error"
            doc_record["error"] = "No chunks generated from content"

    except httpx.HTTPError as e:
        logger.error(f"Failed to fetch URL: {e}")
        doc_record["status"] = "error"
        doc_record["error"] = f"Failed to fetch URL: {str(e)}"
    except Exception as e:
        logger.error(f"Failed to ingest URL content: {e}")
        doc_record["status"] = "error"
        doc_record["error"] = str(e)


async def run_ingestion(
    force: bool,
    regulations: Optional[List[str]],
    ingestion_state: Dict[str, Any],
    all_regulations: List[Dict[str, str]],
):
    """Background task for running full corpus ingestion."""
    try:
        from legal_corpus_ingestion import LegalCorpusIngestion
        ingestion = LegalCorpusIngestion()

        regs_to_process = regulations or [r["code"] for r in all_regulations]

        for i, reg_code in enumerate(regs_to_process):
            ingestion_state["current_regulation"] = reg_code
            ingestion_state["processed"] = i

            try:
                await ingestion.ingest_single(reg_code, force=force)
            except Exception as e:
                logger.error(f"Failed to ingest {reg_code}: {e}")

        ingestion_state["completed"] = True
        ingestion_state["processed"] = len(regs_to_process)

    except Exception as e:
        logger.error(f"Ingestion failed: {e}")
        ingestion_state["error"] = str(e)

    finally:
        ingestion_state["running"] = False
