/*
Copyright 2020 The actions-runner-controller authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
	"github.com/actions/actions-runner-controller/hash"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EphemeralRunnerContainerName is the name of the runner container.
// It represents the name of the container running the self-hosted runner image.
const EphemeralRunnerContainerName = "runner"

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:JSONPath=".spec.githubConfigUrl",name="GitHub Config URL",type=string
// +kubebuilder:printcolumn:JSONPath=".status.runnerId",name=RunnerId,type=number
// +kubebuilder:printcolumn:JSONPath=".status.phase",name=Phase,type=string
// +kubebuilder:printcolumn:JSONPath=".status.jobRepositoryName",name=JobRepository,type=string
// +kubebuilder:printcolumn:JSONPath=".status.jobWorkflowRef",name=JobWorkflowRef,type=string
// +kubebuilder:printcolumn:JSONPath=".status.workflowRunId",name=WorkflowRunId,type=number
// +kubebuilder:printcolumn:JSONPath=".status.jobDisplayName",name=JobDisplayName,type=string
// +kubebuilder:printcolumn:JSONPath=".status.jobId",name=JobId,type=string
// +kubebuilder:printcolumn:JSONPath=".status.message",name=Message,type=string
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// EphemeralRunner is the Schema for the ephemeralrunners API
type EphemeralRunner struct {
	metav1.TypeMeta `json:",inline"`
	// +optional
	metav1.ObjectMeta `json:"metadata,omitempty"`

	// +optional
	Spec EphemeralRunnerSpec `json:"spec,omitempty"`
	// +optional
	Status EphemeralRunnerStatus `json:"status,omitempty"`
}

func (er *EphemeralRunner) IsDone() bool {
	return er.Status.Phase == EphemeralRunnerPhaseSucceeded || er.Status.Phase == EphemeralRunnerPhaseFailed || er.Status.Phase == EphemeralRunnerPhaseOutdated
}

func (er *EphemeralRunner) HasJob() bool {
	return len(er.Status.JobID) > 0
}

func (er *EphemeralRunner) HasContainerHookConfigured() bool {
	for i := range er.Spec.Spec.Containers {
		if er.Spec.Spec.Containers[i].Name != EphemeralRunnerContainerName {
			continue
		}

		for _, env := range er.Spec.Spec.Containers[i].Env {
			if env.Name == "ACTIONS_RUNNER_CONTAINER_HOOKS" {
				return true
			}
		}

		return false
	}
	return false
}

func (er *EphemeralRunner) GitHubConfigSecret() string {
	return er.Spec.GitHubConfigSecret
}

func (er *EphemeralRunner) GitHubConfigUrl() string {
	return er.Spec.GitHubConfigURL
}

func (er *EphemeralRunner) GitHubProxy() *ProxyConfig {
	return er.Spec.Proxy
}

func (er *EphemeralRunner) GitHubServerTLS() *TLSConfig {
	return er.Spec.GitHubServerTLS
}

func (er *EphemeralRunner) VaultConfig() *VaultConfig {
	return er.Spec.VaultConfig
}

func (er *EphemeralRunner) VaultProxy() *ProxyConfig {
	if er.Spec.VaultConfig != nil {
		return er.Spec.VaultConfig.Proxy
	}
	return nil
}

// EphemeralRunnerSpec defines the desired state of EphemeralRunner
type EphemeralRunnerSpec struct {
	// +optional
	GitHubConfigURL string `json:"githubConfigUrl,omitempty"`

	// +optional
	GitHubConfigSecret string `json:"githubConfigSecret,omitempty"`

	// +optional
	GitHubServerTLS *TLSConfig `json:"githubServerTLS,omitempty"`

	// +optional
	RunnerScaleSetID int `json:"runnerScaleSetId,omitempty"`

	// +optional
	Proxy *ProxyConfig `json:"proxy,omitempty"`

	// +optional
	ProxySecretRef string `json:"proxySecretRef,omitempty"`

	// +optional
	VaultConfig *VaultConfig `json:"vaultConfig,omitempty"`

	// +optional
	EphemeralRunnerConfigSecretMetadata *ResourceMeta `json:"ephemeralRunnerConfigSecretMetadata,omitempty"`

	// +optional
	corev1.PodTemplateSpec `json:",inline"`
}

func (s *EphemeralRunnerSpec) Hash() string {
	return hash.ComputeTemplateHash(s)
}

// EphemeralRunnerStatus defines the observed state of EphemeralRunner
type EphemeralRunnerStatus struct {
	// Turns true only if the runner is online.
	// +optional
	Ready bool `json:"ready"`
	// Phase describes phases where EphemeralRunner can be in.
	// The underlying type is a PodPhase, but the meaning is more restrictive
	//
	// The PodFailed phase should be set only when EphemeralRunner fails to start
	// after multiple retries. That signals that this EphemeralRunner won't work,
	// and manual inspection is required
	//
	// The PodSucceded phase should be set only when confirmed that EphemeralRunner
	// actually executed the job and has been removed from the service.
	// +optional
	Phase EphemeralRunnerPhase `json:"phase,omitempty"`
	// +optional
	Reason string `json:"reason,omitempty"`
	// +optional
	Message string `json:"message,omitempty"`

	// +optional
	RunnerID int `json:"runnerId,omitempty"`
	// +optional
	RunnerName string `json:"runnerName,omitempty"`

	// +optional
	Failures map[string]metav1.Time `json:"failures,omitempty"`

	// +optional
	JobRequestID int64 `json:"jobRequestId,omitempty"`

	// +optional
	JobID string `json:"jobId,omitempty"`

	// +optional
	JobRepositoryName string `json:"jobRepositoryName,omitempty"`

	// +optional
	JobWorkflowRef string `json:"jobWorkflowRef,omitempty"`

	// +optional
	WorkflowRunID int64 `json:"workflowRunId,omitempty"`

	// +optional
	JobDisplayName string `json:"jobDisplayName,omitempty"`
}

// EphemeralRunnerPhase is the phase of the ephemeral runner.
// It must be a superset of the pod phase.
type EphemeralRunnerPhase string

const (
	// EphemeralRunnerPhasePending is a phase set when the ephemeral runner is
	// being provisioned and is not yet online.
	EphemeralRunnerPhasePending EphemeralRunnerPhase = "Pending"
	// EphemeralRunnerPhaseRunning is a phase set when the ephemeral runner is online and
	// waiting for a job to execute.
	EphemeralRunnerPhaseRunning EphemeralRunnerPhase = "Running"
	// EphemeralRunnerPhaseSucceeded is a phase set when the ephemeral runner
	// successfully executed the job and has been removed from the service.
	EphemeralRunnerPhaseSucceeded EphemeralRunnerPhase = "Succeeded"
	// EphemeralRunnerPhaseFailed is a phase set when the ephemeral runner
	// fails with unrecoverable failure.
	EphemeralRunnerPhaseFailed EphemeralRunnerPhase = "Failed"
	// EphemeralRunnerPhaseOutdated is a special phase that indicates the runner is outdated and should be upgraded.
	EphemeralRunnerPhaseOutdated EphemeralRunnerPhase = "Outdated"
)

func (s *EphemeralRunnerStatus) LastFailure() metav1.Time {
	var maxTime metav1.Time
	if len(s.Failures) == 0 {
		return maxTime
	}

	for _, ts := range s.Failures {
		if ts.After(maxTime.Time) {
			maxTime = ts
		}
	}
	return maxTime
}

// +kubebuilder:object:root=true

// EphemeralRunnerList contains a list of EphemeralRunner
type EphemeralRunnerList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []EphemeralRunner `json:"items"`
}
