"""Configuration loader for sense_client."""
from __future__ import annotations

import json
import os
import sys

DEFAULTS = {
    "capture": {
        "mode": "screen",
        "target": 0,
        "fps": 2.0,
        "scale": 0.5,
    },
    "detection": {
        "ssimThreshold": 0.92,
        "minArea": 100,
        "roiPadding": 20,
        "cooldownMs": 5000,
    },
    "ocr": {
        "enabled": True,
        "backend": "auto",
        "languages": ["en", "ru"],
        "lang": "eng",
        "psm": 11,
        "minConfidence": 50,
    },
    "gate": {
        "minOcrChars": 20,
        "majorChangeThreshold": 0.85,
        "cooldownMs": 5000,
        "adaptiveCooldownMs": 2000,
        "contextCooldownMs": 10000,
    },
    "relay": {
        "url": "http://localhost:9500",
        "sendThumbnails": True,
        "maxImageKB": 500,
    },
    "vision": {
        "enabled": False,
        "backend": "ollama",
        "model": "llava",
        "ollamaUrl": "http://localhost:11434",
        "timeout": 10.0,
        "throttleSeconds": 5,
        "prompt": "Describe what's on this screen: the application, UI state, any errors or notable content. Be concise (2-3 sentences).",
    },
    "optimization": {
        "backpressure": False,
        "textDedup": False,
        "visionRegionOfInterest": False,
        "shadowValidation": False,
    },
}


def load_config(path: str | None = None) -> dict:
    """Load config from JSON file, merge with defaults."""
    config = json.loads(json.dumps(DEFAULTS))  # deep copy
    if path and os.path.exists(path):
        try:
            with open(path) as f:
                user = json.load(f)
            for section, values in user.items():
                if section in config and isinstance(values, dict):
                    config[section].update(values)
                else:
                    config[section] = values
        except (json.JSONDecodeError, ValueError):
            pass  # use defaults
    return config
