import logging

from dotenv import load_dotenv

from livekit.agents import (
    Agent,
    AgentServer,
    AgentSession,
    AutoSubscribe,
    JobContext,
    MetricsCollectedEvent,
    StopResponse,
    cli,
    inference,
    llm,
    metrics,
    room_io,
)
from livekit.plugins import openai

load_dotenv()

logger = logging.getLogger("transcriber")


# This example demonstrates how to transcribe audio from a remote participant.
# It creates agent session with only STT enabled and publishes transcripts to the room.


class Transcriber(Agent):
    def __init__(self):
        super().__init__(
            instructions="not-needed",
            stt=openai.STT(),
        )

    async def on_user_turn_completed(self, chat_ctx: llm.ChatContext, new_message: llm.ChatMessage):
        user_transcript = new_message.text_content
        logger.info(f" -> {user_transcript}")

        raise StopResponse()


server = AgentServer()


@server.rtc_session()
async def entrypoint(ctx: JobContext):
    logger.info(f"starting transcriber (speech to text) example, room: {ctx.room.name}")
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    session = AgentSession(
        # vad is needed for non-streaming STT implementations
        vad=inference.VAD(model="silero", min_silence_duration=0.3),
    )

    @session.on("metrics_collected")
    def on_metrics_collected(ev: MetricsCollectedEvent):
        metrics.log_metrics(ev.metrics)

    await session.start(
        agent=Transcriber(),
        room=ctx.room,
        room_options=room_io.RoomOptions(
            text_output=True,
            # disable audio output if it's not needed
            audio_output=False,
        ),
    )


if __name__ == "__main__":
    cli.run_app(server)
