from typing import Any


def format_response_dict(audio_path: str, response: Any, stt_provider:str)->dict:
    """Formats the STT response as a dict to be written to json"""

    transcript = list()
    conf = list()
    words_confs = list()
    
    if stt_provider == "google":
        for i in range(len(response.results)):
            alt = response.results[i].alternatives[0]
            transcript.append(alt.transcript.strip())
            conf.append(alt.confidence)
            # adds each word-confidence pair as a tuple to the `words_confs` list
            words_confs.extend(
                [(w_c.word, w_c.confidence) for w_c in list(alt.words)]
            )
    
    elif stt_provider == "ibm":
        for result in response['results']:
            alt = result['alternatives'][0]
            transcript.append(alt['transcript'].strip())
            conf.append(alt['confidence'])
            words_confs.extend(
                [(word, word_conf) for word, word_conf in alt['word_confidence']]
            )
    # filter out hesitation tag
    transcript = " ".join(transcript).replace("%HESITATION ", "")

    return {
        "audio_path": audio_path,
        "transcript": transcript,
        "confidence": conf,
        "words_confidence": words_confs
    }