module Clusters
  module Applications
    class CheckInstallationProgressService < BaseHelmService
      def execute
        return unless app.installing?

        case installation_phase
        when Gitlab::Kubernetes::Pod::SUCCEEDED
          finalize_installation
        when Gitlab::Kubernetes::Pod::FAILED
          on_failed
        else
          check_timeout
        end
      rescue KubeException => ke
        app.make_errored!("Kubernetes error: #{ke.message}") unless app.errored?
      end

      private

      def on_failed
        app.make_errored!(installation_errors || 'Installation silently failed')
        finalize_installation
      end

      def check_timeout
        if Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT
          app.make_errored!('Installation timeouted')
          finalize_installation
        else
          ClusterWaitForAppInstallationWorker.perform_in(
            ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id)
        end
      end

      def finalize_installation
        FinalizeInstallationService.new(app).execute
      end

      def installation_phase
        helm_api.installation_status(app)
      end

      def installation_errors
        helm_api.installation_log(app)
      end
    end
  end
end
