defmodule CodeFundWeb.User.DistributionController do
  use CodeFundWeb, :controller
  use Framework.Controller

  alias CodeFund.Users
  use Framework.Controller.Stub.Definitions, [:index]
  plug(CodeFundWeb.Plugs.RequireAnyRole, roles: ["admin"])

  defconfig do
    [schema: "Distribution", nested: ["User"]]
  end

  def search(conn, %{"user_id" => user_id}) do
    user = Users.get_user!(user_id)
    render(conn, "search.html", user: user)
  end

  def show(conn, _params) do
    conn
    |> redirect(to: user_path(conn, :index))
  end

  defstub new do
    before_hook(&assigns_list/2)
  end

  defstub create do
    before_hook(&assigns_list/2)
    |> success(&success_callback/2)
    |> error(&assigns_list/2)
  end

  defp assigns_list(_conn, %{
         "user_id" => user_id,
         "params" => %{
           "distribution" => %{
             "range_end" => end_date,
             "range_start" => start_date
           }
         }
       }) do
    user = Users.get_user!(user_id)
    impressions = CodeFund.Impressions.distribution_amount(user_id, start_date, end_date)

    [
      action: :create,
      impressions: impressions,
      user: user,
      start_date: start_date,
      end_date: end_date,
      associations: [user_id]
    ]
  end

  defp success_callback(distribution, %{
         "user_id" => user_id,
         "params" => %{
           "distribution" => %{
             "range_end" => end_date,
             "range_start" => start_date
           }
         }
       }) do
    CodeFund.Impressions.by_user_in_date_range(user_id, start_date, end_date)
    |> CodeFund.Repo.update_all(set: [distribution_id: distribution.id])
  end
end
