class Api::V1::Accounts::Captain::InboxesController < Api::V1::Accounts::BaseController
  before_action :current_account
  before_action -> { check_authorization(Captain::Assistant) }

  before_action :set_assistant

  def index
    @captain_inboxes = @assistant.captain_inboxes.includes(:inbox).order(created_at: :desc)
  end

  def create
    inbox = Current.account.inboxes.find(assistant_params[:inbox_id])
    @captain_inbox = @assistant.captain_inboxes.build(
      inbox: inbox,
      captain_unit_id: assistant_params[:captain_unit_id].presence
    )
    @captain_inbox.save!
  end

  def update
    @captain_inbox = @assistant.captain_inboxes.find(permitted_params[:id])
    @captain_inbox.update!(update_params)
  end

  def destroy
    @captain_inbox = @assistant.captain_inboxes.find(permitted_params[:id])
    @captain_inbox.destroy!
    head :no_content
  end

  private

  def set_assistant
    @assistant = account_assistants.find(permitted_params[:assistant_id])
  end

  def account_assistants
    @account_assistants ||= Current.account.captain_assistants
  end

  def permitted_params
    params.permit(:assistant_id, :id, :account_id, :inbox_id)
  end

  def assistant_params
    params.require(:inbox).permit(:inbox_id, :captain_unit_id)
  end

  def update_params
    params.require(:inbox).permit(:always_use_reminder_tool, :captain_unit_id)
  end
end
