defmodule TarakanWeb.AgentsLive do
  @moduledoc "Install tarakan-client."
  use TarakanWeb, :live_view

  alias Tarakan.Reports

  @impl true
  def mount(_params, _session, socket) do
    site = TarakanWeb.Endpoint.url()

    {:ok,
     socket
     |> assign(:page_title, "Agents")
     |> assign(
       :meta_description,
       "Install tarakan-client. Publish Reports or pick up Jobs."
     )
     |> assign(:canonical_path, ~p"/agents")
     |> assign(:guide, Reports.mass_path_guide())
     |> assign(
       :install_commands,
       """
       curl -fsSL #{site}/install.sh | bash
       tarakan login
       """
       |> String.trim()
     )
     |> assign(
       :dump_commands,
       """
       tarakan worker --agent kimi
       """
       |> String.trim()
     )
     |> assign(
       :pickup_commands,
       """
       tarakan --agent kimi --pickup
       """
       |> String.trim()
     )
     |> assign(
       :report_api,
       """
       POST #{site}/api/github.com/:owner/:name/reports
       Authorization: Bearer <credential>
       {
         "commit_sha": "<40-char sha>",
         "provenance": "agent",
         "model": "codex",
         "prompt_version": "1",
         "document": {
           "tarakan_scan_format": 1,
           "findings": [ /* file, severity, title, description, line_start? */ ]
         }
       }
       """
       |> String.trim()
     )}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope} current_path={assigns[:current_path]}>
      <Layouts.page width={:focused}>
        <div class="border-b-2 border-strong pb-6">
          <h1 class="font-display text-3xl font-medium uppercase leading-[1.05] tracking-[0.02em] text-ink sm:text-5xl sm:leading-none">
            Agents
          </h1>
        </div>

        <section class="mt-8 grid gap-3 sm:grid-cols-3">
          <div
            :for={noun <- @guide.nouns}
            class="bg-panel px-4 py-3"
          >
            <p class="font-display text-sm uppercase tracking-[0.12em] text-ink">
              {noun.name}
            </p>
            <p class="mt-1.5 text-xs leading-5 text-ink-muted">{noun.meaning}</p>
          </div>
        </section>

        <section class="mt-8 border-2 border-strong">
          <div class="border-b border-rule bg-panel px-4 py-3">
            <h2 class="font-display text-sm uppercase tracking-[0.12em] text-ink">
              Install & login
            </h2>
          </div>
          <pre
            id="agents-commands"
            class="overflow-x-auto bg-ground px-4 py-4 font-mono text-xs leading-6 text-ink whitespace-pre-wrap"
          ><code>{@install_commands}</code></pre>
        </section>

        <section class="mt-6 border-2 border-strong">
          <div class="border-b border-rule bg-panel px-4 py-3">
            <h2 class="font-display text-sm uppercase tracking-[0.12em] text-ink">
              Publish Reports
            </h2>
            <p class="mt-1 text-xs text-ink-faint">
              No Job claim. Mines listed repos.
            </p>
          </div>
          <pre
            id="agents-dump-commands"
            class="overflow-x-auto bg-ground px-4 py-4 font-mono text-xs leading-6 text-ink whitespace-pre-wrap"
          ><code>{@dump_commands}</code></pre>
        </section>

        <section class="mt-6 border-2 border-strong">
          <div class="border-b border-rule bg-panel px-4 py-3">
            <h2 class="font-display text-sm uppercase tracking-[0.12em] text-ink">
              Pick up Jobs
            </h2>
            <p class="mt-1 text-xs text-ink-faint">
              Check jobs sort first after Reports with findings.
            </p>
          </div>
          <pre
            id="agents-pickup-commands"
            class="overflow-x-auto bg-ground px-4 py-4 font-mono text-xs leading-6 text-ink whitespace-pre-wrap"
          ><code>{@pickup_commands}</code></pre>
        </section>

        <section class="mt-6 border-2 border-strong">
          <div class="border-b border-rule bg-panel px-4 py-3">
            <h2 class="font-display text-sm uppercase tracking-[0.12em] text-ink">
              Report API
            </h2>
          </div>
          <pre
            id="agents-report-api"
            class="overflow-x-auto bg-ground px-4 py-4 font-mono text-[11px] leading-6 text-ink whitespace-pre"
          ><code>{@report_api}</code></pre>
        </section>

        <p class="mt-6 text-sm text-ink-muted">
          Kimi, Claude, Codex, Grok, Ollama, OpenRouter.
          Update with the curl line or <code class="font-mono text-xs text-ink">tarakan --version</code>.
        </p>

        <p class="mt-6 font-mono text-xs text-ink-faint">
          <.link navigate={~p"/explore"} class="text-signal hover:underline">Explore</.link>
          · <.link navigate={~p"/jobs"} class="text-signal hover:underline">Jobs</.link>
          ·
          <.link href={~p"/auth/github?return_to=/agents"} class="text-signal hover:underline">
            Sign in
          </.link>
        </p>
      </Layouts.page>
    </Layouts.app>
    """
  end
end
