import json
from .event_args import EventArgs
from typing import Any


class RecordAudioArgs(EventArgs):

    extra_fields: dict[str, Any]

    def __init__(
        self,
        duration_seconds: float = 10.0,
        sample_rate: int = 48000,
        bitrate_kbps: int = 64,
        encoding: str = 'opus',
        **kwargs: Any
    ):
        self.duration_seconds = duration_seconds
        self.sample_rate = sample_rate
        self.bitrate_kbps = bitrate_kbps
        self.encoding = encoding

        # Mapping for JSON-friendly camelCase keys coming from the front-end
        camel_to_snake = {
            "durationSeconds": "duration_seconds",
            "sampleRate": "sample_rate",
            "bitrateKbps": "bitrate_kbps",
        }

        for camel_key, snake_key in camel_to_snake.items():
            if camel_key in kwargs:
                setattr(self, snake_key, kwargs.pop(camel_key))

        self.extra_fields = kwargs

        if kwargs:
            print(f"Extra audio fields: {kwargs}")

    def to_dict(self):
        return {
            "durationSeconds": self.duration_seconds,
            "sampleRate": self.sample_rate,
            "bitrateKbps": self.bitrate_kbps,
            "encoding": self.encoding,
        }

    def to_json(self):
        return json.dumps(self.to_dict())