defmodule KandeskWeb.Page.Account do
  use KandeskWeb, :live_view
  alias Kandesk.Schema.{User, Board, BoardUser, Column, Task, Comment, Tag}
  alias Kandesk.Repo
  import Ecto.Query
  import Kandesk.Util
  import KandeskWeb.Endpoint, only: [subscribe: 1, unsubscribe: 1, broadcast_from: 4]

  @access_error "Unauthorized access detected"

  def handle_event("show_page", _params, %{assigns: assigns} = socket) do
    user = assigns.user
    changeset = User.changeset(user, %{})
    {:noreply, assign(socket, page: "account", changeset: changeset, edit_row: user)}
  end

  def handle_event("update_password", params, %{assigns: assigns} = socket) do
    %{"user" => form_data} = params
    row = assigns.edit_row
    unless %User{} = row, do: raise(@access_error)

    case row |> User.changeset(form_data) |> Repo.update() do
      {:ok, user} -> handle_event("view_dashboard", nil, assign(socket, user: user))
      {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, changeset: changeset)}
    end
  end

  def handle_event("update_personal_data", params, %{assigns: assigns} = socket) do
    %{"user" => form_data} = params
    row = assigns.edit_row
    unless %User{} = row, do: raise(@access_error)

    case row |> User.admin_changeset(form_data) |> Repo.update() do
      {:ok, user} ->
        handle_event("view_dashboard", nil, assign(socket |> set_locale(user), user: user))

      {:error, %Ecto.Changeset{} = changeset} ->
        {:noreply, assign(socket, changeset: changeset)}
    end
  end

  def handle_event("view_dashboard" = event, params, socket),
    do: KandeskWeb.IndexLive.handle_event(event, params, socket)

  def handle_event("upload_avatar", %{"image" => image}, %{assigns: assigns} = socket) do
    row = assigns.edit_row
    unless %User{} = row, do: raise(@access_error)

    filename = avatar_filename()

    case Repo.update(User.avatar_changeset(row, %{avatar: filename})) do
      {:ok, newrow} ->
        avatar_replace_image(image, row.avatar, filename)
        {:noreply, assign(socket, user: newrow, edit_row: newrow)}

      {:error, changeset} ->
        {:noreply, socket}
    end
  end
end
