class Account::Platform::ApplicationsController

def application_params

Never trust parameters from the scary internet, only allow the white list through.
def application_params
  params.require(:platform_application).permit(
    :name,
    :redirect_uri,
    # 🚅 super scaffolding will insert new fields above this line.
    # 🚅 super scaffolding will insert new arrays above this line.
  )
  # 🚅 super scaffolding will insert processing for new fields above this line.
end

def create

POST /account/teams/:team_id/platform/applications.json
POST /account/teams/:team_id/platform/applications
def create
  respond_to do |format|
    if @application.save
      format.html { redirect_to [:account, @application], notice: I18n.t("platform/applications.notifications.created") }
      format.json { render :show, status: :created, location: [:account, @application] }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @application.errors, status: :unprocessable_entity }
    end
  end
end

def destroy

DELETE /account/platform/applications/:id.json
DELETE /account/platform/applications/:id
def destroy
  @application.destroy
  respond_to do |format|
    format.html { redirect_to [:account, @team, :platform_applications], notice: I18n.t("platform/applications.notifications.destroyed") }
    format.json { head :no_content }
  end
end

def edit

GET /account/platform/applications/:id/edit
def edit
end

def index

GET /account/teams/:team_id/platform/applications.json
GET /account/teams/:team_id/platform/applications
def index
  # if you only want these objects shown on their parent's show page, uncomment this:
  # redirect_to [:account, @team]
end

def new

GET /account/teams/:team_id/platform/applications/new
def new
end

def provision

def provision
  if ENV["TESTING_PROVISION_KEY"].present? && params[:key] == ENV["TESTING_PROVISION_KEY"]
    user = User.create(email: "test@#{SecureRandom.hex}.example.com", password: (password = SecureRandom.hex), password_confirmation: password)
    provision_team = current_user.teams.create(name: "provision-team-#{SecureRandom.hex}", time_zone: user.time_zone)
    test_application = Platform::Application.new(name: "test-application-#{SecureRandom.hex}", team: provision_team)
    if test_application.save
      access_token = test_application.create_access_token
      render json: {message: I18n.t("platform/applications.notifications.test_application_created"), team_id: provision_team.id, access_token: access_token.token}
    else
      render json: {errors: test_application.errors, status: :unprocessable_entity}
    end
  else
    render json: {message: I18n.t("platform/applications.notifications.test_application_failure")}
  end
end

def show

GET /account/platform/applications/:id.json
GET /account/platform/applications/:id
def show
end

def update

PATCH/PUT /account/platform/applications/:id.json
PATCH/PUT /account/platform/applications/:id
def update
  respond_to do |format|
    if @application.update(application_params)
      format.html { redirect_to [:account, @application], notice: I18n.t("platform/applications.notifications.updated") }
      format.json { render :show, status: :ok, location: [:account, @application] }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @application.errors, status: :unprocessable_entity }
    end
  end
end