class Api::V1::InitTemplatesController < Api::V1::ApiController
  protect_from_forgery with: :null_session
  before_action :user_authentication_for_api

  api :GET, "/v1/list_init_templates", "List Init Templates"
    # param :access_token, String, :desc => "User Access Token", :required => true
    param :init_template_name, String, :desc => "Init Template Name"

  def list_init_templates
    init_template_name = params[:init_template_name]
    init_template_id = params[:init_template_id]
    all_init_templates = @user.organisation.init_templates.map{|n| n.json_build}
    if init_template_name
      init_template = @user.organisation.init_templates.where(:name => init_template_name).first
    else
      init_template = @user.organisation.init_templates.where(:id => init_template_id).first
    end
    if init_template_name.present? || init_template_id.present?
      if init_template.present?
        render json: { data: init_template.json_build, status: 200 }
      else
        render json: { msg: "Requested Init Template not found check your init templates", status: 404}  
      end
    else
      render json: { data: { init_templates: all_init_templates }, status: 200}
    end
  end
end