package jira

import (
	"fmt"
	"log"
)

type AlertType int

const (
	BackupFailed AlertType = iota
	BackupTooLong
	RestoreFailed
	RestoreTooLong
	ImportFailed
	ImportTooLong
	ClusterCreationTooLong
	ClusterScalingTooLong
	ClusterPausingTooLong
	ClusterResumingTooLong
)

type SearchParams struct {
	Tp        AlertType
	Id        string
	ClusterId string
	TenantId  string
}

func (p SearchParams) toJql() string {
	ret := ""
	switch p.Tp {
	case BackupFailed:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND summary ~ TiDBClusterBackupStatusFailed AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case BackupTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND summary ~ TiDBClusterBackupDurationTooLong AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case RestoreFailed:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task  AND summary ~ TiDBClusterRestoreStatusFailed AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case RestoreTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND summary ~ TiDBClusterRestoreDurationMoreThan12h AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ImportFailed:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task  AND (summary ~ ImportFail OR summary ~ ImportCriticalFail) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ImportTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND (summary ~ ImportStilSyncingAfterLongTime) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ClusterCreationTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND (summary ~ InfraClusterCreationDurationTooLong OR summary ~ TheProjectsFirstInfraClusterCreationDurationTooLong) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ClusterScalingTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND (summary ~ InfraClusterScalingDurationMoreThan1h) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ClusterPausingTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND (summary ~ InfraClusterPauseDurationTooLong) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	case ClusterResumingTooLong:
		ret = fmt.Sprintf("project in (O11Y,TCA) AND issueType = Task AND (summary ~ InfraClusterResumeDurationTooLong) AND (\"Tenant ID\" ~ %s OR \"Cluster ID\" ~ %s) ORDER BY created DESC", p.TenantId, p.ClusterId)
	}
	//ret := "project in (O11Y,TCA) AND created >= -30m ORDER BY created DESC"
	log.Println("jql:", ret)
	return ret
}
