/*
Copyright 2022 The KodeRover 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 job

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/koderover/zadig/pkg/microservice/aslan/config"
	commonmodels "github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
	"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb"
	"github.com/koderover/zadig/pkg/setting"
)

type JiraJob struct {
	job      *commonmodels.Job
	workflow *commonmodels.WorkflowV4
	spec     *commonmodels.JiraJobSpec
}

func (j *JiraJob) Instantiate() error {
	j.spec = &commonmodels.JiraJobSpec{}
	if err := commonmodels.IToiYaml(j.job.Spec, j.spec); err != nil {
		return err
	}
	j.job.Spec = j.spec
	return nil
}

func (j *JiraJob) SetPreset() error {
	j.spec = &commonmodels.JiraJobSpec{}
	if err := commonmodels.IToi(j.job.Spec, j.spec); err != nil {
		return err
	}
	j.job.Spec = j.spec
	return nil
}

func (j *JiraJob) MergeArgs(args *commonmodels.Job) error {
	j.spec = &commonmodels.JiraJobSpec{}
	if err := commonmodels.IToi(args.Spec, j.spec); err != nil {
		return err
	}
	j.job.Spec = j.spec
	return nil
}

func (j *JiraJob) ToJobs(taskID int64) ([]*commonmodels.JobTask, error) {
	resp := []*commonmodels.JobTask{}
	j.spec = &commonmodels.JiraJobSpec{}
	if err := commonmodels.IToi(j.job.Spec, j.spec); err != nil {
		return resp, err
	}
	if len(j.spec.Issues) == 0 {
		return nil, errors.New("需要指定至少一个 Jira Issue")
	}
	j.job.Spec = j.spec
	info, err := mongodb.NewProjectManagementColl().GetJira()
	if err != nil {
		return nil, errors.Errorf("get jira info error: %v", err)
	}
	for _, issue := range j.spec.Issues {
		issue.Link = fmt.Sprintf("%s/browse/%s", info.JiraHost, issue.Key)
	}
	jobTask := &commonmodels.JobTask{
		Name: j.job.Name,
		Key:  j.job.Name,
		JobInfo: map[string]string{
			JobNameKey: j.job.Name,
		},
		JobType: string(config.JobJira),
		Spec: &commonmodels.JobTaskJiraSpec{
			ProjectID:    j.spec.ProjectID,
			IssueType:    j.spec.IssueType,
			Issues:       j.spec.Issues,
			TargetStatus: j.spec.TargetStatus,
		},
		Timeout: 0,
	}
	return []*commonmodels.JobTask{jobTask}, nil
}

func (j *JiraJob) LintJob() error {
	j.spec = &commonmodels.JiraJobSpec{}
	if err := commonmodels.IToiYaml(j.job.Spec, j.spec); err != nil {
		return err
	}
	switch j.spec.Source {
	case setting.VariableSourceRuntime, setting.VariableSourceOther:
	default:
		return errors.New("invalid source")
	}
	return nil
}
