class Admin::Api::ApplicationPlansController < Admin::Api::ServiceBaseController
  representer ApplicationPlan
  wrap_parameters ApplicationPlan

  # swagger (this is the unnested "fast track" route to get all app_plans)
  ##~ sapi = source2swagger.namespace("Account Management API")
  ##~ e = sapi.apis.add
  ##~ e.path = "/admin/api/application_plans.xml"
  ##~ e.responseClass = "List[application_plan]"
  #
  ##~ op = e.operations.add
  ##~ op.httpMethod = "GET"
  ##~ op.summary    = "Application Plan List (all services)"
  ##~ op.description = "Returns the list of all application plans across services. Note that application plans are scoped by service."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  #
  ##~ e = sapi.apis.add
  ##~ e.path = "/admin/api/services/{service_id}/application_plans.xml"
  ##~ e.responseClass = "List[application_plan]"
  #
  ##~ op = e.operations.add
  ##~ op.httpMethod = "GET"
  ##~ op.summary    = "Application Plan List"
  ##~ op.description = "Returns the list of all application plans of a service."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  #
  def index
    respond_with(application_plans)
  end

  ##~ op = e.operations.add
  ##~ op.httpMethod = "POST"
  ##~ op.summary    = "Application Plan Create"
  ##~ op.description = "Creates an application plan."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  ##~ op.parameters.add :name => "name", :description => "Name of the application plan.", :dataType => "string", :required => true, :paramType => "query"
  ##~ op.parameters.add @parameter_system_name_by_name
  ##~ op.parameters.add @parameter_extra_short
  #
  def create
    application_plan = application_plans.create(application_plan_params)
    application_plan.transition_to(params[:state])

    respond_with(application_plan)
  end

  ##~ e = sapi.apis.add
  ##~ e.path = "/admin/api/services/{service_id}/application_plans/{id}.xml"
  ##~ e.responseClass = "application_plan"
  #
  ##~ op = e.operations.add
  ##~ op.httpMethod = "GET"
  ##~ op.summary    = "Application Plan Read"
  ##~ op.description = "Returns and application plan."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  ##~ op.parameters.add @parameter_application_plan_id_by_id
  #
  def show
    respond_with(application_plan)
  end

  ##~ op = e.operations.add
  ##~ op.httpMethod = "PUT"
  ##~ op.summary    = "Application Plan Update"
  ##~ op.description = "Updates an application plan."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  ##~ op.parameters.add @parameter_application_plan_id_by_id
  ##~ op.parameters.add :name => "name", :description => "Name of the application plan.", :dataType => "string", :paramType => "query"
  ##~ op.parameters.add @parameter_extra_short
  #
  def update
    application_plan.update_attributes(application_plan_params)
    application_plan.transition_to(params[:state])

    respond_with(application_plan)
  end

  ##~ op            = e.operations.add
  ##~ op.httpMethod = "DELETE"
  ##~ op.summary    = "Application Plan Delete"
  ##~ op.description = "Deletes an application plan."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  ##~ op.parameters.add @parameter_application_plan_id_by_id
  #
  def destroy
    application_plan.destroy

    respond_with(application_plan)
  end

  # swagger
  ##~ e = sapi.apis.add
  ##~ e.path   = "/admin/api/services/{service_id}/application_plans/{id}/default.xml"
  ##~ e.responseClass = "application_plan"
  #
  ##~ op            = e.operations.add
  ##~ op.httpMethod = "PUT"
  ##~ op.summary    = "Application Plan Set to Default"
  ##~ op.description = "Makes the application plan the default one. New applications will be assigned to the default plan unless an application_plan_id is explicity passed (e.g. on the signup express operation)."
  ##~ op.group = "application_plan"
  #
  ##~ op.parameters.add @parameter_access_token
  ##~ op.parameters.add @parameter_service_id_by_id_name
  ##~ op.parameters.add @parameter_application_plan_id_by_id
  #
  def default
    service.update_attribute(:default_application_plan, application_plan)
    respond_with(application_plan)
  end

  protected

  def application_plan_params
    params.fetch(:application_plan)
  end

  def application_plans
    @application_plans ||= scope.application_plans.where(issuer: accessible_services)
  end

  def application_plan
    @application_plan ||= application_plans.find(params[:id])
  end
end
