class Admin::Communication::Sparkpost::BounceDomainsController < Admin::Communication::Controller
  def show
    render_success :ok, json: setting_props
  end

  def create
    if setting.update(setting_params)
      render_success :ok, json: setting_props
    else
      render_error :unprocessable_entity, errors: setting
    end
  end

  def update
    if setting.verify
      render_success :ok, json: setting_props
    else
      render_error :unprocessable_entity, errors: setting
    end
  end

  private
    def setting
      @setting ||= current_school.find_or_build_communication_sparkpost_bounce_config
    end

    def setting_params
      params.permit(:domain)
    end

    def setting_props
      {
        id: setting.id,
        event: setting.event,
        domain: setting.domain,
        verified: setting.verified,
        hostname: setting.domain,
        dns_type: 'CNAME',
        value: 'sparkpostmail.com',
        updated_at: setting.updated_at
      }
    end
end
