"""High-level reconstruction orchestration.

Wraps the Rust engine with file discovery, progress reporting,
and optional cleanup.

Example:
    from gdeltnews_rs import reconstruct

    reconstruct(
        "gdeltdata",
        "output",
        language="en",
        url_filters=["reuters.com", "nytimes.com"],
    )
"""

from __future__ import annotations

import glob
import os
from typing import List, Optional, Union

from tqdm import tqdm

from gdeltnews_rs._gdeltnews_rs import reconstruct_file, reconstruct_file_to_jsonl, reconstruct_files


def _find_input_files(input_dir: str) -> List[str]:
    """Find all .gz and .json GDELT Web NGrams files in a directory."""
    patterns = [
        os.path.join(input_dir, "*.webngrams.json.gz"),
        os.path.join(input_dir, "*.webngrams.json"),
    ]
    files = []
    for pattern in patterns:
        files.extend(glob.glob(pattern))
    files.sort()
    return files


def reconstruct(
    input_dir: str,
    output_dir: str,
    *,
    language: Optional[str] = "en",
    url_filters: Optional[Union[str, List[str]]] = None,
    num_threads: Optional[int] = None,
    delete_gz: bool = False,
    delete_empty: bool = True,
    show_progress: bool = True,
) -> int:
    """Reconstruct articles from GDELT Web NGrams files.

    Processes all .gz and .json files in input_dir, writes one JSONL
    file per input file to output_dir.

    Args:
        input_dir: Directory containing downloaded GDELT files.
        output_dir: Directory to write JSONL output files.
        language: Language code to filter by (e.g. "en"). None keeps all.
        url_filters: URL substring(s) to filter by. None keeps all.
        num_threads: Number of threads. None uses all cores.
        delete_gz: Delete .gz files after processing.
        delete_empty: Delete empty output files.
        show_progress: Show progress information.

    Returns:
        Total number of articles reconstructed.
    """
    # Normalize url_filters
    if isinstance(url_filters, str):
        url_filters = [url_filters]

    input_files = _find_input_files(input_dir)
    if not input_files:
        print(f"No GDELT Web NGrams files found in {input_dir}")
        return 0

    os.makedirs(output_dir, exist_ok=True)

    if show_progress:
        print(f"Found {len(input_files)} files to process")

    # Use the Rust parallel engine
    total = reconstruct_files(
        input_files,
        output_dir,
        language=language,
        url_filters=url_filters,
        num_threads=num_threads,
    )

    if show_progress:
        print(f"Reconstructed {total} articles from {len(input_files)} files")

    # Optional cleanup
    if delete_gz:
        for f in input_files:
            if f.endswith(".gz"):
                os.remove(f)

    if delete_empty:
        for f in glob.glob(os.path.join(output_dir, "*.jsonl")):
            if os.path.getsize(f) == 0:
                os.remove(f)

    return total


__all__ = ["reconstruct"]
