"""Shared runtime paths for the installed Superpitch app."""

from __future__ import annotations

import os
import shutil
from pathlib import Path

APP_NAME = "Superpitch"
APP_SLUG = "superpitch"
APP_HOME_ENV = "SUPERPITCH_HOME"

PACKAGE_ROOT = Path(__file__).resolve().parent
ASSET_ROOT = PACKAGE_ROOT / "assets"


def app_home() -> Path:
    override = os.environ.get(APP_HOME_ENV, "").strip()
    if override:
        return Path(override).expanduser()
    return Path.home() / f".{APP_SLUG}"


APP_HOME = app_home()
DATA_DIR = APP_HOME / "data"
MODELS_DIR = APP_HOME / "models"
CACHE_DIR = APP_HOME / "cache"

TRAIN_FILE = DATA_DIR / "train.jsonl"
VAL_FILE = DATA_DIR / "val.jsonl"
FEEDBACK_FILE = DATA_DIR / "feedback.jsonl"
HISTORY_FILE = DATA_DIR / "history.jsonl"
CONFIG_FILE = APP_HOME / "config.json"

LATEST_MODEL_FILE = MODELS_DIR / "latest.pkl"
LEARNED_MODEL_FILE = MODELS_DIR / "learned.pkl"

BUNDLED_TRAIN_FILE = ASSET_ROOT / "data" / "train.jsonl"
BUNDLED_VAL_FILE = ASSET_ROOT / "data" / "val.jsonl"
BUNDLED_MODEL_FILE = ASSET_ROOT / "models" / "latest.pkl"


def ensure_app_dirs() -> None:
    for path in (APP_HOME, DATA_DIR, MODELS_DIR, CACHE_DIR):
        path.mkdir(parents=True, exist_ok=True)


def bootstrap_user_files() -> list[Path]:
    ensure_app_dirs()
    copied: list[Path] = []
    for src, dst in (
        (BUNDLED_TRAIN_FILE, TRAIN_FILE),
        (BUNDLED_VAL_FILE, VAL_FILE),
        (BUNDLED_MODEL_FILE, LATEST_MODEL_FILE),
    ):
        if dst.exists() or not src.exists():
            continue
        shutil.copy2(src, dst)
        copied.append(dst)
    return copied
