class Api::V1::Accounts::Captain::PricingsController < Api::V1::Accounts::BaseController
  before_action :fetch_pricing, only: [:show, :update, :destroy]

  def index
    @pricings = Current.account.captain_pricings.includes(:brand, :inbox)
  end

  def show; end

  def create
    @pricing = Current.account.captain_pricings.new(pricing_params.except(:inbox_ids))
    @pricing.save!
    sync_inboxes(@pricing, pricing_params[:inbox_ids])
    render :show, status: :created
  end

  def update
    @pricing.update!(pricing_params.except(:inbox_ids))
    sync_inboxes(@pricing, pricing_params[:inbox_ids])
    render :show
  end

  def destroy
    @pricing.destroy!
    head :no_content
  end

  private

  def fetch_pricing
    @pricing = Current.account.captain_pricings.find(params[:id])
  end

  def pricing_params
    params.require(:pricing).permit(
      :captain_brand_id,
      :inbox_id,
      :day_range,
      :suite_category,
      :duration,
      :price,
      :keywords,
      inbox_ids: []
    )
  end

  def sync_inboxes(pricing, inbox_ids)
    return if inbox_ids.nil?

    ids = Array(inbox_ids).reject(&:blank?).map(&:to_i)
    if ids.empty?
      pricing.inboxes.clear
      return
    end

    inboxes = Current.account.inboxes.where(id: ids)
    pricing.inboxes = inboxes
  end
end
