"""Configuration du service audioin."""

import json
from dataclasses import dataclass
from typing import Dict, Any


@dataclass
class AudioConfig:
    """Configuration audio principale."""

    sample_rate: int = 16000
    channels: int = 1
    frame_size: int = 320  # 20ms à 16kHz
    device_name: str = "respeaker-2mic"


@dataclass
class VADConfig:
    """Configuration du Voice Activity Detector."""

    threshold: float = 0.3
    attack_ms: int = 80
    release_ms: int = 200
    eos_ms: int = 800  # End of Speech

    def to_dict(self) -> Dict[str, Any]:
        """Convertit en dictionnaire."""
        return {
            "threshold": self.threshold,
            "attack_ms": self.attack_ms,
            "release_ms": self.release_ms,
            "eos_ms": self.eos_ms,
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "VADConfig":
        """Crée depuis un dictionnaire."""
        return cls(
            threshold=data.get("threshold", 0.3),
            attack_ms=data.get("attack_ms", 80),
            release_ms=data.get("release_ms", 200),
            eos_ms=data.get("eos_ms", 800),
        )

    @classmethod
    def from_json(cls, json_str: str) -> "VADConfig":
        """Crée depuis une chaîne JSON."""
        return cls.from_dict(json.loads(json_str))


@dataclass
class MQTTConfig:
    """Configuration MQTT."""

    host: str = "127.0.0.1"
    port: int = 1883
    topic_rms: str = "audio/rms"
    topic_vad: str = "audio/vad"
    topic_capabilities: str = "audio/capabilities"
    topic_config: str = "audio/vad/config"
    client_id: str = "skull-audioin"
    keepalive: int = 60


@dataclass
class ServiceConfig:
    """Configuration complète du service."""

    audio: AudioConfig
    vad: VADConfig
    mqtt: MQTTConfig
    log_file: str = "/opt/Skull/logs/audioin.log"

    def __init__(self):
        self.audio = AudioConfig()
        self.vad = VADConfig()
        self.mqtt = MQTTConfig()
