defmodule PhilomenaWeb.Api.Json.Search.PostController do
  use PhilomenaWeb, :controller

  alias PhilomenaQuery.Search
  alias Philomena.Posts.Post
  alias Philomena.Posts.Query
  import Ecto.Query

  def index(conn, params) do
    user = conn.assigns.current_user

    case Query.compile(params["q"], user: user) do
      {:ok, query} ->
        posts =
          Post
          |> Search.search_definition(
            %{
              query: %{
                bool: %{
                  must: [
                    query,
                    %{term: %{deleted: false}},
                    %{term: %{access_level: "normal"}}
                  ]
                }
              },
              sort: %{created_at: :desc}
            },
            conn.assigns.pagination
          )
          |> Search.search_records(preload(Post, [:user, :topic]))

        conn
        |> put_view(PhilomenaWeb.Api.Json.Forum.Topic.PostView)
        |> render("index.json", posts: posts, total: posts.total_entries)

      {:error, msg} ->
        conn
        |> put_status(:bad_request)
        |> json(%{error: msg})
    end
  end
end
