/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 integration

import (
	"context"
	"fmt"

	batchv1 "k8s.io/api/batch/v1"
	corev1 "k8s.io/api/core/v1"

	ctrl "sigs.k8s.io/controller-runtime/pkg/client"

	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
	"github.com/apache/camel-k/v2/pkg/client"
	"github.com/apache/camel-k/v2/pkg/util/kubernetes"
)

type cronJobController struct {
	obj              *batchv1.CronJob
	integration      *v1.Integration
	client           client.Client
	lastCompletedJob *batchv1.Job
}

var _ controller = &cronJobController{}

func (c *cronJobController) checkReadyCondition(ctx context.Context) (bool, error) {
	// Check latest job result
	lastScheduleTime := c.obj.Status.LastScheduleTime

	if lastScheduleTime == nil || len(c.obj.Status.Active) != 0 {
		return false, nil
	}

	jobs := batchv1.JobList{}
	if err := c.client.List(ctx, &jobs,
		ctrl.InNamespace(c.integration.Namespace),
		ctrl.MatchingLabels{v1.IntegrationLabel: c.integration.Name},
	); err != nil {
		return true, err
	}
	t := lastScheduleTime.Time
	for i, job := range jobs.Items {
		if job.Status.Active == 0 && job.CreationTimestamp.Time.Before(t) {
			continue
		}
		c.lastCompletedJob = &jobs.Items[i]
		t = c.lastCompletedJob.CreationTimestamp.Time
	}
	if c.lastCompletedJob != nil {
		if failed := kubernetes.GetJobCondition(*c.lastCompletedJob, batchv1.JobFailed); failed != nil &&
			failed.Status == corev1.ConditionTrue {
			c.integration.SetReadyCondition(corev1.ConditionFalse,
				v1.IntegrationConditionLastJobFailedReason,
				fmt.Sprintf("last job %s failed: %s", c.lastCompletedJob.Name, failed.Message))
			c.integration.Status.Phase = v1.IntegrationPhaseError

			return true, nil
		}
	}

	return false, nil
}

func (c *cronJobController) updateReadyCondition(readyPods int32) bool {
	switch {
	case c.obj.Status.LastScheduleTime == nil:
		c.integration.SetReadyCondition(corev1.ConditionTrue,
			v1.IntegrationConditionCronJobCreatedReason, "cronjob created")

		return true

	case len(c.obj.Status.Active) > 0:
		c.integration.SetReadyCondition(corev1.ConditionTrue,
			v1.IntegrationConditionCronJobActiveReason, "cronjob active")

		return true

	case c.obj.Spec.SuccessfulJobsHistoryLimit != nil && *c.obj.Spec.SuccessfulJobsHistoryLimit == 0 &&
		c.obj.Spec.FailedJobsHistoryLimit != nil && *c.obj.Spec.FailedJobsHistoryLimit == 0:
		c.integration.SetReadyCondition(corev1.ConditionTrue,
			v1.IntegrationConditionCronJobCreatedReason, "no jobs history available")

		return true

	case c.lastCompletedJob != nil:
		if complete := kubernetes.GetJobCondition(*c.lastCompletedJob, batchv1.JobComplete); complete != nil &&
			complete.Status == corev1.ConditionTrue {
			c.integration.SetReadyCondition(corev1.ConditionTrue,
				v1.IntegrationConditionLastJobSucceededReason,
				fmt.Sprintf("last job %s completed successfully", c.lastCompletedJob.Name))

			return true
		}

	default:
		c.integration.SetReadyCondition(corev1.ConditionUnknown, "", "")
	}

	return false
}

func (c *cronJobController) hasTemplateIntegrationLabel() bool {
	return c.obj.Spec.JobTemplate.Spec.Template.Labels[v1.IntegrationLabel] != ""
}

func (c *cronJobController) getControllerName() string {
	return "CronJob/" + c.obj.Name
}
