"""Configuration management utilities."""

from pathlib import Path

from .console import get_console

# Default config directory
CONFIG_DIR = Path.home() / ".sagellm"
DEFAULT_CONFIG = CONFIG_DIR / "config.yaml"


def ensure_config_dir() -> None:
    """Ensure ~/.sagellm directory exists."""
    CONFIG_DIR.mkdir(parents=True, exist_ok=True)


def init_default_config() -> None:
    """Initialize default config if not exists."""
    ensure_config_dir()
    if not DEFAULT_CONFIG.exists():
        default_content = """# sageLLM Configuration
# Generated by sagellm CLI
# Docs: https://github.com/IntelliStream/sagellm

backend:
  kind: cpu  # Options: cpu, cuda, ascend
    device: cpu

engine:
    kind: cpu
    model: sshleifer/tiny-gpt2
    model_path: sshleifer/tiny-gpt2

workload:
  segments:
    - name: short
      prompt_tokens: 128
      output_tokens: 128
    - name: long
      prompt_tokens: 2048
      output_tokens: 512
    - name: stress
      prompt_tokens: 512
      output_tokens: 256
      concurrency: 4

output:
  metrics_path: metrics.json
  report_path: report.md

# Feature flags (Year 2+)
# enable_fp8: false
# enable_int4: false
# enable_prefix_cache: false
# enable_spec_decode: false
"""
        DEFAULT_CONFIG.write_text(default_content)
        get_console().print(f"✅ Created config: {DEFAULT_CONFIG}", style="green")
