import sys
from pathlib import Path
import yaml

CONFIG_PATH = Path.home() / ".acquiscan.yaml"

DEFAULT_CONFIG = """\
# Required: SEC EDGAR requires declaring a user agent for all requests.
# https://www.sec.gov/about/webmaster-frequently-asked-questions#developers
user_agent: "Your Name (your@email.com)"

# Optional: only needed for --mode llm
# llm_api_key: "sk-..."
# llm_model: "gpt-5-mini"
"""

DEFAULTS = {
    "user_agent": None,
    "llm_api_key": None,
    "llm_model": "gpt-5-mini",
}

class Config:
    CONFIG_PATH = CONFIG_PATH

    def __init__(self, **kwargs):
        self.user_agent = kwargs.get("user_agent", DEFAULTS["user_agent"])
        self.llm_api_key = kwargs.get("llm_api_key", DEFAULTS["llm_api_key"])
        self.llm_model = kwargs.get("llm_model", DEFAULTS["llm_model"])

    @classmethod
    def load(cls):
        if not CONFIG_PATH.exists():
            CONFIG_PATH.write_text(DEFAULT_CONFIG, encoding="utf-8")
            print(
                f"Created config file: {CONFIG_PATH}\n"
                f"Please edit it with your details and run again.",
                file=sys.stderr,
            )
            sys.exit(1)

        with open(CONFIG_PATH, encoding="utf-8") as f:
            data = yaml.safe_load(f) or {}

        config = cls(**data)
        if not config.user_agent or "your@email.com" in config.user_agent.lower():
            print(
                f"Please set your user_agent in {CONFIG_PATH}",
                file=sys.stderr,
            )
            sys.exit(1)
        return config
