package dispatch

type FieldOptionItem struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type FieldOptionConditionOperator struct {
	Name        ConditionOperator `json:"name"`
	Description string            `json:"description"`
}

type FieldOptionRuleCondition struct {
	IssueFields               []FieldOptionItem              `json:"issue_fields"`
	SingleValueOperators      []FieldOptionConditionOperator `json:"single_value_operators"`
	MultiValueOperators       []FieldOptionConditionOperator `json:"multi_value_operators"`
	NoValueOperators          []FieldOptionConditionOperator `json:"no_value_operators"`
	WeeklyTimeWindowOperators []FieldOptionConditionOperator `json:"weekly_time_window_operators"`
	CalendarEventOperators    []FieldOptionConditionOperator `json:"calendar_event_operators"`
	CalendarNames             []string                       `json:"calendar_names"`
}

type FieldOptionAction struct {
	Types                 []FieldOptionItem  `json:"types"`
	BotEmails             []string           `json:"bot_emails"`
	OnCallTeams           []OnCallTeamOption `json:"on_call_teams"`
	ReceiverMappingFields []string           `json:"receiver_mapping_fields"`
}

type OnCallTeamOption struct {
	TeamKey  string `json:"team_key"`
	TeamName string `json:"team_name"`
}

type FieldOption struct {
	RuleCondition FieldOptionRuleCondition `json:"rule_condition"`
	Action        FieldOptionAction        `json:"action"`
}

func DefaultFieldOption() FieldOption {
	return FieldOption{
		RuleCondition: FieldOptionRuleCondition{
			IssueFields: []FieldOptionItem{
				{Name: FieldOptionNow, Description: "The current condition evaluation time."},
				{Name: FieldOptionIssueKey, Description: "The Jira issue key."},
				{Name: FieldOptionIssueProjectKey, Description: "The Jira project key."},
				{Name: FieldOptionIssuePriority, Description: "The Jira issue priority."},
				{Name: FieldOptionIssueCustomerDeploymentModel, Description: "The customer deployment model: cloud or on_premise."},
				{Name: FieldOptionIssueCustomerClusterID, Description: "The customer cluster ID. Only supported for projects ST, TCID, TCOC, TCAB, TCT, and O11Y."},
				{Name: FieldOptionIssueCustomerTenantID, Description: "The customer tenant or account ID."},
				{Name: FieldOptionIssueCustomerTenantRegion, Description: "The customer tenant region."},
				{Name: FieldOptionIssueCustomFieldsOnCallTier, Description: "The on-call support tier configured on the issue."},
				{Name: FieldOptionIssueAutoOncallStatus, Description: "The terminal Auto Oncall Status configured on the Jira issue."},
				{Name: FieldOptionIssueCustomFieldsSeverity, Description: "The severity configured on the Jira issue."},
				{Name: FieldOptionIssueAssigneeEmail, Description: "The email address of the Jira issue assignee."},
				{Name: FieldOptionIssueReporterEmail, Description: "The email address of the Jira issue reporter."},
				{Name: FieldOptionIssueCreatedAt, Description: "The time when the Jira issue was created."},
				{Name: FieldOptionIssueComputedResponseSLADueAt, Description: "The response SLA due time. Only supported for projects GTOC, TCOC, and TCT; 0 means unlimited."},
				{Name: FieldOptionIssueComponents, Description: "All Jira components configured on the issue."},
				{Name: FieldOptionIssuePrimaryComponent, Description: "The first Jira component configured on the issue."},
				{Name: FieldOptionIssueComputedEffectivePrimaryComponent, Description: "The effective primary component: the first component from the latest triage result when available, otherwise the first Jira component."},
				{Name: FieldOptionIssueSummary, Description: "The Jira issue summary."},
				{Name: FieldOptionTriageIssueType, Description: "The issue type from the latest triage result."},
				{Name: FieldOptionTriageComponents, Description: "All components from the latest triage result."},
				{Name: FieldOptionTriagePrimaryComponent, Description: "The first component from the latest triage result."},
				{Name: FieldOptionTriageSeverity, Description: "The severity from the latest triage result."},
				{Name: FieldOptionTriageProjectLevel, Description: "Whether the issue is project-level according to the latest triage result."},
				{Name: FieldOptionTriageLanguage, Description: "The language from the latest triage result."},
			},
			SingleValueOperators: []FieldOptionConditionOperator{
				{Name: ConditionOperatorEqual, Description: "Matches when the field equals the configured value."},
				{Name: ConditionOperatorNotEqual, Description: "Matches when the field does not equal the configured value."},
				{Name: ConditionOperatorRegex, Description: "Matches when any field value matches the configured regular expression."},
				{Name: ConditionOperatorContains, Description: "Matches when any field value contains the configured value."},
			},
			MultiValueOperators: []FieldOptionConditionOperator{
				{Name: ConditionOperatorIn, Description: "Matches when any field value is included in the configured values."},
				{Name: ConditionOperatorNotIn, Description: "Matches when no field value is included in the configured values."},
			},
			NoValueOperators: []FieldOptionConditionOperator{
				{Name: ConditionOperatorExists, Description: "Matches when the field exists."},
				{Name: ConditionOperatorNotExists, Description: "Matches when the field does not exist."},
			},
			WeeklyTimeWindowOperators: []FieldOptionConditionOperator{
				{Name: ConditionOperatorWithinWeeklyTimeWindow, Description: "Matches when the field time is within the configured weekly time window."},
				{Name: ConditionOperatorOverlapsWeeklyTimeWindow, Description: "Matches when the interval from evaluation time to the field time overlaps the configured weekly time window."},
			},
			CalendarEventOperators: []FieldOptionConditionOperator{
				{Name: ConditionOperatorWithinCalendarEvent, Description: "Matches when the field time is within an event in the configured calendar."},
			},
		},
		Action: FieldOptionAction{
			Types: []FieldOptionItem{
				{Name: string(ActionTypeAssign), Description: "Select an eligible engineer from the configured receiver, set the Jira issue assignee, update the engineer's dispatch state, and remove auto-assign labels from the Jira issue."},
				{Name: string(ActionTypeAssignL2), Description: "Select an eligible engineer from the configured receiver, set the Jira issue L2 assignee field, update the engineer's dispatch state, and remove auto-assign labels from the Jira issue."},
				{Name: string(ActionTypeEscalate), Description: "Transition the Jira issue to ESCALATE TO L3 and remove auto-assign labels."},
				{Name: string(ActionTypeRetrigger), Description: "Schedule the issue to run through Dispatch again after the configured delay."},
				{Name: string(ActionTypeNoop), Description: "Exit the Execution early when used with the rule's on-success terminate transition, or simulate a successful or failed Action for testing. Performs no side effects."},
				{Name: string(ActionTypeTransitionIssue), Description: "Transition the Jira issue to the configured target status."},
				{Name: string(ActionTypeSendWebRequest), Description: "Send an HTTP request with a configurable method, URL, headers, and body."},
				{Name: string(ActionTypeTriage), Description: "Analyze the Jira issue and generate triage results for subsequent rules."},
				{Name: string(ActionTypeSendAssignmentNotification), Description: "Send the current assignment target a Feishu card and consume the role notification label."},
				{Name: string(ActionTypeMarkAssignmentMessageUrgent), Description: "Mark the Ticket Manager assignment card urgent in Feishu."},
				{Name: string(ActionTypePageAssignment), Description: "Page the assignment target once and wait for acknowledgement."},
				{Name: string(ActionTypeEscalateAssignmentToLeader), Description: "Escalate the assignment role to the target's direct leader at most once."},
			},
			ReceiverMappingFields: []string{
				FieldOptionIssueProjectKey,
				FieldOptionIssuePriority,
				FieldOptionIssueCustomerDeploymentModel,
				FieldOptionIssueCustomerClusterID,
				FieldOptionIssueCustomerTenantID,
				FieldOptionIssueCustomerTenantRegion,
				FieldOptionIssueCustomFieldsOnCallTier,
				FieldOptionIssueReporterEmail,
				FieldOptionIssuePrimaryComponent,
				FieldOptionIssueComputedEffectivePrimaryComponent,
				FieldOptionIssueSummary,
			},
		},
	}
}

const (
	FieldOptionNow                                    = "now"
	FieldOptionIssueKey                               = "issue.key"
	FieldOptionIssueProjectKey                        = "issue.project_key"
	FieldOptionIssuePriority                          = "issue.priority"
	FieldOptionIssueCustomerDeploymentModel           = "issue.customer.deploy_model"
	FieldOptionIssueCustomerClusterID                 = "issue.customer.cluster_id"
	FieldOptionIssueCustomerTenantID                  = "issue.customer.tenant_id"
	FieldOptionIssueCustomerTenantRegion              = "issue.customer.tenant_region"
	FieldOptionIssueCustomFieldsOnCallTier            = "issue.custom_fields.oncall_tier"
	FieldOptionIssueAutoOncallStatus                  = "issue.auto_oncall_status"
	FieldOptionIssueCustomFieldsSeverity              = "issue.custom_fields.severity"
	FieldOptionIssueAssigneeEmail                     = "issue.assignee_email"
	FieldOptionIssueReporterEmail                     = "issue.reporter_email"
	FieldOptionIssueCreatedAt                         = "issue.created_at"
	FieldOptionIssueComponents                        = "issue.components"
	FieldOptionIssuePrimaryComponent                  = "issue.primary_component"
	FieldOptionIssueSummary                           = "issue.summary"
	FieldOptionIssueComputedResponseSLADueAt          = "issue.computed.response_sla_due_at"
	FieldOptionIssueComputedEffectivePrimaryComponent = "issue.computed.effective_primary_component"
	FieldOptionTriageIssueType                        = "issue.triage.issue_type"
	FieldOptionTriageComponents                       = "issue.triage.components"
	FieldOptionTriagePrimaryComponent                 = "issue.triage.primary_component"
	FieldOptionTriageSeverity                         = "issue.triage.severity"
	FieldOptionTriageProjectLevel                     = "issue.triage.project_level"
	FieldOptionTriageLanguage                         = "issue.triage.language"
)
