"""Core entry point: start_core() launches the watchdog + inbox poll loop."""

import logging
import time
from pathlib import Path

from watchdog.observers import Observer

from armorer.core.handler import ArmorerCoreHandler

logger = logging.getLogger("ArmorerCore")


def start_core(installed_root: str, config_dir: str):
    logger.info(f"🛡️  Armorer Core Active.")
    logger.info(f"📍 Monitoring Agents: {installed_root}")
    logger.info(f"📥 Monitoring Inbox: {config_dir}/inbox")

    # Ensure watched directories exist to prevent watchdog startup crashes.
    Path(installed_root).mkdir(parents=True, exist_ok=True)
    config_path = Path(config_dir)
    handler = ArmorerCoreHandler(config_path)

    observer = Observer()
    observer.schedule(handler, installed_root, recursive=True)
    observer.schedule(handler, str(config_path / "inbox"), recursive=False)
    observer.start()

    try:
        while True:
            handler._poll_inbox()
            time.sleep(1)
    except KeyboardInterrupt:
        pass
    finally:
        observer.stop()
        observer.join()
