"""
Clone static index pool data into stage resources for EDA integrator.
"""

import logging
import shutil
from pathlib import Path

logger = logging.getLogger(__name__)

# Package dir: stage_processing/
_STAGE_PROCESSING_DIR = Path(__file__).resolve().parent
STATIC_INDEX_POOL_DATA = _STAGE_PROCESSING_DIR / "static" / "index_pool_data.json"

INDEX_POOLS_DIR_NAME = "indexpools"
INDEX_POOL_DATA_FILENAME = "index_pool_data.json"


def run_clone_index_pool_data(stage_dir: str | Path) -> None:
    """
    Copy static/index_pool_data.json to stage_dir/resources/indexpools/index_pool_data.json.
    Creates stage_dir/resources/indexpools if needed.
    """
    stage = Path(stage_dir).resolve()
    resources = stage / "resources"
    indexpools_dir = resources / INDEX_POOLS_DIR_NAME
    dest_file = indexpools_dir / INDEX_POOL_DATA_FILENAME

    if not STATIC_INDEX_POOL_DATA.exists():
        logger.warning(
            "Static index pool data not found at %s; skipping clone.",
            STATIC_INDEX_POOL_DATA,
        )
        return

    indexpools_dir.mkdir(parents=True, exist_ok=True)
    shutil.copy2(STATIC_INDEX_POOL_DATA, dest_file)
    logger.info("Cloned index pool data to %s", dest_file)
