#!/bin/bash

# Function to print usage information
usage() {
	echo "Usage: $0 [record|transcription] <options>"
	echo ""
	echo "Actions:"
	echo "  record        Record audio from a selected source"
	echo "  transcription Transcribe audio from a .wav file"
	echo ""
	echo "Options for 'record':"
	echo "  -s, --source  Specify the audio source (required)"
	echo ""
	echo "Options for 'transcription':"
	echo "  -f, --file    Specify the audio file to transcribe (required)"
	echo "  -l, --lang    Specify the audio file language (default: en)"
	exit 1
}

if [[ $# -eq 0 ]]; then
	usage
fi

# Check for required environment variable
if [[ -z "${LLM_API_KEY}" ]]; then
	echo "The environment variable LLM_API_KEY is not set."
	echo 'You can use the following command: export $(xargs < ../.env))'
	exit 1
fi

ACTION=$1
shift

host=${STT_API_HOST:-"http://localhost:8001"}
LANG="en" # Default language

if [ "$ACTION" == "record" ]; then
	if [ "$#" -eq 0 ]; then
		echo "Error: Source is required for record action."
		echo "Available sources:"
		pactl list short sources | awk '{print $2}'
		exit 1
	fi

	SOURCE=""
	while [[ "$#" -gt 0 ]]; do
		case $1 in
		-s | --source)
			SOURCE="$2"
			shift
			;;
		*)
			echo "Unknown parameter passed: $1"
			usage
			;;
		esac
		shift
	done

	# Validate the provided source
	if ! pactl list short sources | awk '{print $2}' | grep -q "^$SOURCE$"; then
		echo "Error: Invalid audio source. Available sources:"
		pactl list short sources | awk '{print $2}'
		exit 1
	fi

	timestamp=$(date +"%Y%m%d_%H%M%S")
	filename="record_${timestamp}.wav"
	echo "Start recording to ${filename} ; use CTRL+C to terminate."
	parec -d "${SOURCE}" --file-format=wav "${filename}"
elif [ "$ACTION" == "transcription" ]; then
	if [ "$#" -eq 0 ]; then
		echo "Error: File is required for transcription action."
		usage
	fi

	FILE=""
	while [[ "$#" -gt 0 ]]; do
		case $1 in
		-f | --file)
			FILE="$2"
			shift
			;;
		-l | --lang)
			LANG="$2"
			shift
			;;
		*)
			echo "Unknown parameter passed: $1"
			usage
			;;
		esac
		shift
	done

	if [ -z "$FILE" ]; then
		echo "Error: File is required for transcription action."
		usage
	fi

	# Check if the file exists
	if [ ! -f "$FILE" ]; then
		echo "Error: File '$FILE' does not exist."
		exit 1
	fi

	# Ensure that curl is available
	if ! command -v curl &>/dev/null; then
		echo "curl is required for transcription but could not be found on your system. Please install it."
		exit 1
	fi

	# Transcribe the specified file
	echo "Transcribing file $FILE, be patient"
	curl "${host}/v1/audio/transcriptions" -H "Authorization: Bearer ${LLM_API_KEY}" \
		-F "file=@${FILE}" \
		-F "stream=true" \
		-F "language=${LANG}"
else
	usage
fi
