from __future__ import annotations

import logging

import azure.functions as func
import azure.functions.durable_functions as df

from app.services.durable_service import build_greeting

durable_blueprint = func.Blueprint()  # type: ignore[no-untyped-call]


@durable_blueprint.route(
    route="orchestrators/{functionName}",
    methods=["POST"],
    auth_level=func.AuthLevel.ANONYMOUS,
)
@durable_blueprint.durable_client_input(client_name="client")
async def http_start(
    req: func.HttpRequest, client: df.DurableOrchestrationClient
) -> func.HttpResponse:
    instance_id = await client.start_new(req.route_params["functionName"])
    logging.info("Started orchestration with ID '%s'.", instance_id)
    return client.create_check_status_response(req, instance_id)


@durable_blueprint.orchestration_trigger(context_name="context")
def hello_orchestrator(context: df.DurableOrchestrationContext) -> list[str]:
    results: list[str] = []
    results.append(yield context.call_activity("say_hello", "Tokyo"))
    results.append(yield context.call_activity("say_hello", "Seattle"))
    results.append(yield context.call_activity("say_hello", "London"))
    return results


@durable_blueprint.activity_trigger(input_name="city")
def say_hello(city: str) -> str:
    return build_greeting(city)
