defmodule A2aAgentWebWeb.SummarizerController do
  @moduledoc """
  Controller for summarizing customer feedback using OpenAI.
  """
  use A2aAgentWebWeb, :controller

  @doc """
  POST /api/summarize
  Receives JSON {"text": ...} and returns a summary.
  """
  @spec summarize(Plug.Conn.t(), map()) :: Plug.Conn.t()
  @spec summarize(Plug.Conn.t(), map()) :: Plug.Conn.t()
  def summarize(conn, %{"text" => text}) do
    graph = %{summarize: %{operator: A2aAgentWebWeb.Operators.LLMOperator, deps: []}}
    agent_map = %{}
    input = %{"prompt_template" => "Summarize this customer feedback: ~s", "context" => %{"feedback" => text}}

    result = HypergraphAgent.BasicOrchestrator.orchestrate(graph, agent_map, input)

    require Logger
    Logger.error("summarize orchestrator result: #{inspect(result)}")

    case result do
      {:ok, %{:summarize => %{"summary" => summary}}} ->
        json(conn, %{"summary" => summary})
      {:ok, %{"summarize" => %{"summary" => summary}}} ->
        json(conn, %{"summary" => summary})
      {:ok, %{summarize: {:ok, summary}}} ->
        json(conn, %{"summary" => summary})
      {:ok, %{"summarize" => {:ok, summary}}} ->
        json(conn, %{"summary" => summary})
      {:ok, %{"summarize" => summary}} when is_binary(summary) ->
        json(conn, %{"summary" => summary})
      {:ok, other} ->
        Logger.error("Unexpected orchestrator result in summarize: #{inspect(other)}")
        conn |> put_status(500) |> json(%{"error" => "Unexpected result: #{inspect(other)}", "raw" => inspect(other)})
      {:error, reason} ->
        Logger.error("summarize error: #{inspect(reason)}")
        conn |> put_status(500) |> json(%{"error" => inspect(reason)})
      other ->
        Logger.error("summarize received unhandled result: #{inspect(other)}")
        conn |> put_status(500) |> json(%{"error" => "Unhandled result: #{inspect(other)}"})
    end
  end

  @doc """
  POST /api/story
  Receives JSON {"text": ...} and returns a story generated by the LLM.
  """
  @spec make_story(Plug.Conn.t(), map()) :: Plug.Conn.t()
  def make_story(conn, %{"text" => text}) do
    graph = %{story: %{operator: A2aAgentWebWeb.Operators.LLMOperator, deps: []}}
    agent_map = %{}
    input = %{"prompt_template" => "Write a short story about: ~s", "context" => %{topic: text}}

    result = HypergraphAgent.BasicOrchestrator.orchestrate(graph, agent_map, input)

    require Logger
    Logger.error("make_story orchestrator result: #{inspect(result)}")

    case result do
      {:ok, %{:story => %{"summary" => story}}} ->
        json(conn, %{"story" => story})
      {:ok, %{"story" => %{"summary" => story}}} ->
        json(conn, %{"story" => story})
      {:ok, %{"story" => story}} when is_binary(story) ->
        json(conn, %{"story" => story})
      {:ok, %{story: {:ok, story}}} ->
        json(conn, %{"story" => story})
      {:ok, %{"story" => {:ok, story}}} ->
        json(conn, %{"story" => story})
      {:ok, other} ->
        Logger.error("Unexpected orchestrator result in make_story: #{inspect(other)}")
        conn |> put_status(500) |> json(%{"error" => "Unexpected result: #{inspect(other)}", "raw" => inspect(other)})
      {:error, reason} ->
        Logger.error("make_story error: #{inspect(reason)}")
        conn |> put_status(500) |> json(%{"error" => inspect(reason)})
      other ->
        Logger.error("make_story received unhandled result: #{inspect(other)}")
        conn |> put_status(500) |> json(%{"error" => "Unhandled result: #{inspect(other)}"})
    end
  end
end
