defmodule MvWeb.UserLive.Show do
  @moduledoc """
  LiveView for displaying a single user's details.

  ## Features
  - Display user information (email, OIDC ID)
  - Show authentication methods (password, OIDC)
  - Display linked member account (if exists)
  - Navigate to edit form
  - Return to user list

  ## Displayed Information
  - Email address
  - OIDC ID (if authenticated via OIDC)
  - Password authentication status
  - Linked member (name and email)

  ## Authentication Status
  Shows which authentication methods are enabled for the user:
  - Password authentication (has hashed_password)
  - OIDC authentication (has oidc_id)

  ## Navigation
  - Back to user list
  - Edit user (with return_to parameter for back navigation)
  """
  use MvWeb, :live_view

  import MvWeb.LiveHelpers, only: [current_actor: 1]

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_user={@current_user}>
      <.header>
        {gettext("User")} {@user.email}
        <:subtitle>{gettext("This is a user record from your database.")}</:subtitle>

        <:actions>
          <.button navigate={~p"/users"} aria-label={gettext("Back to users list")}>
            <.icon name="hero-arrow-left" />
            <span class="sr-only">{gettext("Back to users list")}</span>
          </.button>
          <%= if can?(@current_user, :update, @user) do %>
            <.button
              variant="primary"
              navigate={~p"/users/#{@user}/edit?return_to=show"}
              data-testid="user-edit"
            >
              <.icon name="hero-pencil-square" /> {gettext("Edit User")}
            </.button>
          <% end %>
        </:actions>
      </.header>

      <.list>
        <:item title={gettext("Email")}>{@user.email}</:item>
        <:item title={gettext("Role")}>{@user.role.name}</:item>
        <:item title={gettext("Password Authentication")}>
          {if MvWeb.Helpers.UserHelpers.has_password?(@user),
            do: gettext("Enabled"),
            else: gettext("Not enabled")}
        </:item>
        <:item title={gettext("OIDC")}>
          {if MvWeb.Helpers.UserHelpers.has_oidc?(@user),
            do: gettext("Linked"),
            else: gettext("Not linked")}
        </:item>
        <:item title={gettext("Linked Member")}>
          <%= if @user.member do %>
            <.link
              navigate={~p"/members/#{@user.member}"}
              class="text-blue-600 underline hover:text-blue-800"
            >
              <.icon name="hero-users" class="inline w-4 h-4 mr-1" />
              {MvWeb.Helpers.MemberHelpers.display_name(@user.member)}
            </.link>
          <% else %>
            <span class="italic text-gray-500">{gettext("No member linked")}</span>
          <% end %>
        </:item>
      </.list>
    </Layouts.app>
    """
  end

  @impl true
  def mount(%{"id" => id}, _session, socket) do
    actor = current_actor(socket)

    user =
      Ash.get!(Mv.Accounts.User, id, domain: Mv.Accounts, load: [:member, :role], actor: actor)

    if Mv.Helpers.SystemActor.system_user?(user) do
      {:ok,
       socket
       |> put_flash(:error, gettext("This user cannot be viewed."))
       |> push_navigate(to: ~p"/users")}
    else
      {:ok,
       socket
       |> assign(:page_title, gettext("Show User"))
       |> assign(:user, user)}
    end
  end
end
