"""
配置加载

从 scripts/data/config.json 读取用户配置，从环境变量读取服务端配置（LLM + 系统 URL）。
"""

import json
import os
from pathlib import Path

_CONFIG_PATH = Path(__file__).parent.parent.parent / "scripts" / "data" / "config.json"


def load_config() -> dict[str, str | Path]:
    """加载并合并配置，缺失字段使用默认值"""
    raw = {}
    if _CONFIG_PATH.exists():
        with open(_CONFIG_PATH, encoding="utf-8") as f:
            raw = json.load(f)

    project_root = _CONFIG_PATH.parent

    return {
        "sso_login_url": os.environ.get("SSO_LOGIN_URL", "https://tyrz.fynu.edu.cn/sso/login"),
        "portal_url": os.environ.get("PORTAL_URL", "https://tyrz.fynu.edu.cn/oshall"),
        "reimburse_url": os.environ.get("REIMBURSE_URL", "http://210.45.32.214:8081"),
        "reimburse_page": os.environ.get("REIMBURSE_PAGE", "/expen/common/common?v=4.0"),
        "travel_page": os.environ.get("TRAVEL_PAGE", "/expen/travel/travel?v=4.0"),
        "username": raw.get("username", ""),
        "password": raw.get("password", ""),
        "default_name": raw.get("default_name", ""),
        "default_card_no": raw.get("default_card_no", ""),
        "default_person_id": raw.get("default_person_id", ""),
        "consumable_storage": raw.get("consumable_storage", "躬行楼 C205"),
        "attachment_dir": project_root / "attachments",
    }


def get_llm_config() -> dict[str, str]:
    """加载 LLM 配置，优先从环境变量读取，缺失字段使用默认值"""
    return {
        "model": os.environ.get("LLM_MODEL", "qwen-vl-max"),
        "api_base": os.environ.get("LLM_API_BASE", "http://localhost:8080/v1"),
        "api_key": os.environ.get("LLM_API_KEY", "lm-studio"),
    }
