defmodule FirehoseWeb.BlogLive.Show do
  @moduledoc """
  Example LiveView for showing a single blog post.

  Copy this into your host app and customize the layout/styling.
  """
  use MyAppWeb, :live_view

  import Blogex.Components

  @impl true
  def mount(%{"blog" => blog_id} = _params, _session, socket) do
    blog = Blogex.get_blog!(String.to_existing_atom(blog_id))
    {:ok, assign(socket, :blog, blog)}
  end

  @impl true
  def handle_params(%{"slug" => slug}, _uri, socket) do
    blog = socket.assigns.blog
    post = blog.get_post!(slug)

    meta = Blogex.SEO.meta_tags(post, FirehoseWeb.Endpoint.url(), blog)

    {:noreply,
     socket
     |> assign(:post, post)
     |> assign(:meta, meta)
     |> assign(:page_title, post.title)
     |> assign(:base_path, blog.base_path())}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div class="max-w-3xl mx-auto py-8 px-4">
      <nav class="mb-8">
        <a href={@base_path} class="text-blue-600 hover:underline">
          &larr; Back to {@blog.title()}
        </a>
      </nav>

      <.post_show post={@post} />

      <footer class="mt-12 pt-8 border-t border-gray-200">
        <h3 class="text-lg font-semibold mb-4">Tags</h3>
        <.tag_list tags={@post.tags} base_path={@base_path} />
      </footer>
    </div>
    """
  end
end
