/*
** Job Arranger for ZABBIX
** Copyright (C) 2025 Daiwa Institute of Research Ltd. All Rights Reserved.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

package main

import (
	"fmt"
	"jobarranger2/src/libs/golibs/builder"
	"jobarranger2/src/libs/golibs/common"
	"strings"
)

func BuildSQlConditionForHoldIconUserActionUnhold(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodWait).
				Field("status", common.OpEq, common.StatusReady).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForHoldIconUserActionSkip(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodWait).
				Field("status", common.OpEq, common.StatusReady).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForRunErrIconUserActionSkip(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodNormal).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodAbort).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodRerun).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForEndErrorUserActionSkip(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("status", common.OpEq, common.StatusEndErr).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForRunErrIconUserActionRerun(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodNormal).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodAbort).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodRerun).
				Field("status", common.OpEq, common.StatusRunErr).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForReadyIconUserActionForceStop(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodNormal).
				Field("status", common.OpEq, common.StatusReady).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodRerun).
				Field("status", common.OpEq, common.StatusReady).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodAbort).
				Field("status", common.OpEq, common.StatusReady).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForRunningIconUserActionForceStop(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodNormal).
				Field("status", common.OpEq, common.StatusRun).
				Build(),
		).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodRerun).
				Field("status", common.OpEq, common.StatusRun).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForAbortingIconUserActionForceStop(innerJobId uint64) common.SQLCondition {
	condSql := fmt.Sprintf("select method_flag, status from %s where inner_job_id = %d;", common.Ja2RunJobTable, innerJobId)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("method_flag", common.OpEq, common.MethodAbort).
				Field("status", common.OpEq, common.StatusAbort).
				Build(),
		).
		DefaultAction(common.ActionIgnore).
		Build()

	return sqlCondt
}

func BuildSQlConditionForHostLockCheck(hostnames ...string) common.SQLCondition {
	// Build 'host1','host2','host3'
	quoted := make([]string, len(hostnames))
	for i, h := range hostnames {
		quoted[i] = fmt.Sprintf("'%s'", h)
	}

	inClause := strings.Join(quoted, ",")
	condSql := fmt.Sprintf("select count(*) as count from %s where lock_host_name in (%s);", common.Ja2HostLockTable, inClause)
	sqlCondt := builder.NewSQLCondition(condSql).
		AddCondition(
			builder.NewCondition(common.ActionExec).
				Field("count", common.OpLt, 1).
				Build(),
		).
		DefaultAction(common.ActionWait).
		Build()

	return sqlCondt
}
