defmodule ToweropsWeb.HealthController do
  @moduledoc false

  use ToweropsWeb, :controller

  alias Ecto.Adapters.SQL
  alias Towerops.Repo

  def index(conn, _params) do
    # Basic health check with database connectivity test
    case SQL.query(Repo, "SELECT 1", []) do
      {:ok, _} ->
        conn
        |> put_resp_content_type("application/json")
        |> send_resp(
          200,
          Jason.encode!(%{
            status: "ok",
            database: "connected",
            version: :towerops |> Application.spec(:vsn) |> to_string()
          })
        )

      {:error, _} ->
        conn
        |> put_resp_content_type("application/json")
        |> send_resp(
          503,
          Jason.encode!(%{
            status: "error",
            database: "disconnected"
          })
        )
    end
  end
end
