import typer
import logging
from adsbpy.prettylogging import setup_logger

logger = setup_logger()

app = typer.Typer()


@app.command()
def fetch(file: str, delete_ignore: bool = False):
    from pathlib import Path
    from adsbpy import fetch
    import asyncio

    IGNORE_FILE = Path("adsbpy/.ignore")
    if delete_ignore and IGNORE_FILE.exists():
        logger.warning(f"Deleting ignore file at {IGNORE_FILE}")
        IGNORE_FILE.unlink()
    ignore_list = []
    if IGNORE_FILE.exists():
        logger.info(f"Found ignore file at {IGNORE_FILE}")
        with open(IGNORE_FILE) as f:
            ignore_list = [line.strip() for line in f.readlines() if line.strip()]
    else:
        logger.info("No ignore file found, starting fresh.")
        IGNORE_FILE.parent.mkdir(parents=True, exist_ok=True)
        IGNORE_FILE.touch()
    
    with open(file) as f:
        lines = f.readlines()
    lines = [line for line in lines if not line.strip() in ignore_list]
    asyncio.run(fetch.process_days_concurrently(lines))
    logger.info("Fetching process completed.")


if __name__ == "__main__":
    app()
