#
# Copyright (c) 2024-2026, Daily
#
# SPDX-License-Identifier: BSD 2-Clause License
#

"""A keypad-driven phone menu (IVR) bot.

The caller interacts entirely with DTMF keypresses, no speech. A
``DTMFAggregator`` turns each key sequence into a ``DTMF: ...`` transcription
that the LLM reacts to:

- press 1 for business hours
- press 2 for the office location
- press 3 to request a callback: the caller enters their callback number and
  presses ``#`` to finalize it (the aggregator's termination digit, which
  flushes the sequence immediately instead of waiting for the idle timeout)

DTMF arrives as ``InputDTMFFrame`` from the transport (e.g. a telephony provider,
or the eval harness), so there's no STT in the pipeline. The bot speaks its
responses with TTS, like a real phone menu would.
"""

import os

from dotenv import load_dotenv
from loguru import logger

from pipecat.evals.transport import EvalTransportParams
from pipecat.frames.frames import LLMRunFrame
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.worker import PipelineParams, PipelineWorker, ProcessorUnusablePolicy
from pipecat.processors.aggregators.dtmf_aggregator import DTMFAggregator
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.processors.aggregators.llm_response_universal import LLMContextAggregatorPair
from pipecat.runner.types import RunnerArguments
from pipecat.runner.utils import create_transport
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.transports.base_transport import BaseTransport, TransportParams
from pipecat.transports.daily.transport import DailyParams
from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams
from pipecat.workers.runner import WorkerRunner

load_dotenv(override=True)

SYSTEM_INSTRUCTION = """You are an automated phone menu for Acme Corp.

The caller interacts only with their phone keypad. Each keypress arrives as a \
message like "DTMF: 1" (the digits they pressed, possibly ending in #).

When the call starts, greet the caller and read this menu: press 1 for our \
business hours, press 2 for our location, press 3 to request a callback.

When the caller presses 1, tell them Acme Corp is open from 9 AM to 5 PM, Monday \
through Friday. When they press 2, tell them Acme Corp is located at 123 Main \
Street. When they press 3, ask them to enter their callback number followed by \
the pound key. For any other key, say that's not a valid option and read the \
menu again.

While collecting a callback number, the digits may arrive split across several \
messages if the caller pauses; the number is complete only when a message ends \
in #. Once you have the complete number, read it back digit by digit, tell the \
caller someone will call them back, and read the menu again.

Keep every response short. Your responses may be read aloud, so don't use emojis \
or formatting."""

# We use lambdas to defer transport parameter creation until the transport
# type is selected at runtime.
transport_params = {
    "eval": lambda: EvalTransportParams(
        audio_in_enabled=True,
        audio_out_enabled=True,
    ),
    "daily": lambda: DailyParams(
        audio_in_enabled=True,
        audio_out_enabled=True,
    ),
    "twilio": lambda: FastAPIWebsocketParams(
        audio_in_enabled=True,
        audio_out_enabled=True,
    ),
    "webrtc": lambda: TransportParams(
        audio_in_enabled=True,
        audio_out_enabled=True,
    ),
}


async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
    logger.info("Starting bot")

    llm = OpenAILLMService(
        api_key=os.environ["OPENAI_API_KEY"],
        settings=OpenAILLMService.Settings(system_instruction=SYSTEM_INSTRUCTION),
    )

    tts = CartesiaTTSService(
        api_key=os.environ["CARTESIA_API_KEY"],
        settings=CartesiaTTSService.Settings(
            voice="86e30c1d-714b-4074-a1f2-1cb6b552fb49",
        ),
    )

    context = LLMContext()
    user_aggregator, assistant_aggregator = LLMContextAggregatorPair(context)

    pipeline = Pipeline(
        [
            transport.input(),  # Transport user input (incl. DTMF keypresses)
            DTMFAggregator(),  # InputDTMFFrame -> "DTMF: ..." TranscriptionFrame
            user_aggregator,  # User (keypad) turns
            llm,  # LLM
            tts,  # TTS
            transport.output(),  # Transport bot output
            assistant_aggregator,  # Assistant responses
        ]
    )

    worker = PipelineWorker(
        pipeline,
        params=PipelineParams(
            enable_metrics=True,
            enable_usage_metrics=True,
        ),
        idle_timeout_secs=runner_args.pipeline_idle_timeout_secs,
        processor_unusable_policy=ProcessorUnusablePolicy.END,
    )

    runner = WorkerRunner(handle_sigint=runner_args.handle_sigint)

    await runner.add_workers(worker)

    @transport.event_handler("on_client_connected")
    async def on_client_connected(transport, client):
        logger.info("Client connected")
        # Kick off the call with the menu greeting.
        context.add_message({"role": "developer", "content": "Greet the caller and read the menu."})
        await worker.queue_frames([LLMRunFrame()])

    @transport.event_handler("on_client_disconnected")
    async def on_client_disconnected(transport, client):
        logger.info("Client disconnected")
        await runner.cancel()

    await runner.run()


async def bot(runner_args: RunnerArguments):
    """Main bot entry point compatible with Pipecat Cloud."""
    transport = await create_transport(runner_args, transport_params)
    await run_bot(transport, runner_args)


if __name__ == "__main__":
    from pipecat.runner.run import main

    main()
