defmodule ToweropsWeb.ApiDocsController do
  @moduledoc """
  Controller for API documentation pages.
  """
  use ToweropsWeb, :controller

  alias Towerops.ApiTokens

  def index(conn, _params) do
    # Check if user is logged in and has an organization selected
    sample_token =
      case conn.assigns[:current_scope] do
        %{user: user, organization: org} when not is_nil(user) and not is_nil(org) ->
          # User is logged in with an organization - check for API tokens
          tokens = ApiTokens.list_organization_api_tokens(org.id)

          if Enum.empty?(tokens) do
            "your-api-token-here (create one in your organization settings)"
          else
            token = List.first(tokens)
            "your-api-token-here (use: #{token.name})"
          end

        _ ->
          # Not logged in or no organization selected
          "YOUR_API_TOKEN"
      end

    render(conn, :index, sample_token: sample_token)
  end
end
