defmodule TarakanWeb.PageLive do
  @moduledoc """
  Static policy and pricing pages.

  These have no interactivity, but they are LiveViews so navigating to them
  from the nav is a live patch rather than a full page reload. As plain
  controller pages they were the only routes in the shell without a LiveSocket,
  which made them behave differently from everything around them.

  One module, one action per page, templates in `page_live/`.
  """
  use TarakanWeb, :live_view

  embed_templates "page_live/*"

  @impl true
  def mount(_params, _session, socket) do
    {:ok, assign(socket, :security_contact, Application.get_env(:tarakan, :security_contact))}
  end

  @impl true
  def handle_params(_params, _uri, socket) do
    {:noreply, apply_action(socket, socket.assigns.live_action)}
  end

  defp apply_action(socket, :disclosure) do
    socket
    |> assign(:page_title, "Disclosure policy")
    |> assign(
      :meta_description,
      "Tarakan is public disclosure by default: visibility levels, verification quorum, " <>
        "vendor notification, and abuse reporting."
    )
    |> assign(:canonical_path, ~p"/policies/disclosure")
  end

  defp apply_action(socket, :content) do
    socket
    |> assign(:page_title, "Content policy")
    |> assign(
      :meta_description,
      "What may and may not be published to the Tarakan public security record, and how " <>
        "takedowns and appeals work."
    )
    |> assign(:canonical_path, ~p"/policies/content")
  end

  defp apply_action(socket, :pricing) do
    take_rate_percent =
      Tarakan.Market.take_rate()
      |> Decimal.mult(100)
      |> Decimal.normalize()
      |> Decimal.to_string(:normal)

    socket
    |> assign(:page_title, "Pricing")
    |> assign(:take_rate_percent, take_rate_percent)
    |> assign(
      :meta_description,
      "Tarakan is free: read, submit, and check security findings at no cost. Post a " <>
        "contract to get specific code reviewed; Tarakan keeps #{take_rate_percent}% when " <>
        "the work is accepted."
    )
    |> assign(:canonical_path, ~p"/pricing")
  end

  defp apply_action(socket, :managed_disclosure) do
    socket
    |> assign(:page_title, "Managed disclosure")
    |> assign(
      :meta_description,
      "Managed disclosure: Tarakan staff triage findings with the vendor under an SLA. " <>
        "Handling, not suppression - the public record is unchanged."
    )
    |> assign(:canonical_path, ~p"/services/disclosure")
  end

  @impl true
  def render(%{live_action: :disclosure} = assigns), do: disclosure(assigns)
  def render(%{live_action: :content} = assigns), do: content(assigns)
  def render(%{live_action: :pricing} = assigns), do: pricing(assigns)
  def render(%{live_action: :managed_disclosure} = assigns), do: managed_disclosure(assigns)
end
