defmodule BanchanWeb.AccountDisabledLive do
  @moduledoc """
  Page shown to disabled users.
  """
  use BanchanWeb, :live_view

  alias Banchan.Repo

  alias BanchanWeb.Components.{Layout, RichText}

  @impl true
  def mount(_params, _session, socket) do
    user = socket.assigns.current_user && Repo.preload(socket.assigns.current_user, :disable_info)

    if is_nil(user) || is_nil(user.disable_info) do
      {:ok, socket |> push_navigate(to: Routes.login_path(Endpoint, :new))}
    else
      {:ok, socket |> assign(current_user: user)}
    end
  end

  @impl true
  def render(assigns) do
    ~F"""
    <Layout flashes={@flash} padding={0}>
      <div class="w-full h-full bg-base-200">
        <div class="w-full max-w-7xl p-4 mx-auto">
          <div class="text-xl">
            Account Disabled
          </div>
          <div :if={@current_user.disable_info.disabled_until}>
            Your account has been disabled until {@current_user.disabled_until |> Timex.to_datetime() |> Timex.format!("{RFC822}")}.
          </div>
          <div :if={is_nil(@current_user.disable_info.disabled_until)}>
            Your account has been indefinitely disabled.
          </div>
          <div :if={@current_user.disable_info.disabled_reason}>
            <div class="divider" />
            <div class="text-xl">
              Reason
            </div>
            <RichText content={@current_user.disable_info.disabled_reason} />
          </div>
        </div>
      </div>
    </Layout>
    """
  end
end
