#!/bin/bash
source $HOME/.gamerc

[ "$1" == "" ] && exec echo "Usage: $0 <Environment (TEST => 0, LIVE => 1)>"

if ! type function_loaded &> /dev/null; then
	. ./additional_functions
fi

env=$1
ENV_TAG=$(get_env_tag_output ${env})
output_filename="/tmp/fb_probability_monitor.log"
table_name="fortune_bag"
table_name_output="${table_name} (福袋表)"

stop_check=$(check_table_uploading $table_name)
while true; do
	if [ "${stop_check}" -eq 0 ]; then
		echo "Waiting ${table_name_output} ..."
		stop_check=$(check_table_uploading $table_name)
	elif [ "${stop_check}" -eq -1 ]; then
		echo "Quit ${table_name_output} ..."
		break
	elif [ "${stop_check}" -eq 1 ]; then
		echo "Checking ${table_name_output} ..."

		# 一般福袋
		COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set BETWEEN 1 AND 98 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer <> 100 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

		if [ "${COUNT}" -ne 0 ]; then
			OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set BETWEEN 1 AND 98 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer <> 100 ORDER BY id, set) TO '${output_filename}' CSV HEADER")
    			send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (一般) 設定機率異常"
		fi

		# 特殊福袋1
		COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 99 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer >= 100 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

		if [ "${COUNT}" -ne 0 ]; then
			OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 99 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer >= 100 ORDER BY id, set) TO '${output_filename}' CSV HEADER")
			send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (一般非必得) 設定機率異常"
		fi

		# 特殊福袋2
		COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set BETWEEN 101 AND 110 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer % 100 <> 0 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

		if [ "${COUNT}" -ne 0 ]; then
			OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set = 101 AND 110 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT id, set, SUM(probability) FROM fortune_bag WHERE (id, set) IN (SELECT id, set FROM t1 ORDER BY id, set) GROUP BY id, set HAVING SUM(probability)::integer % 100 <> 0 ORDER BY id, set) TO '${output_filename}' CSV HEADER")
			send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 (自選) 設定機率異常"
		fi

		## 新增規則1
		COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag GROUP BY id, set HAVING COUNT(set) >= 1), t2 AS (SELECT id, SUM(CASE WHEN set <= 100 THEN 1 ELSE 0 END) AS f1, SUM(CASE WHEN set > 100 THEN 1 ELSE 0 END) AS f3 FROM t1 GROUP BY id) SELECT COUNT(id) FROM t2 WHERE f1 > 0 AND f3 > 0) TO STDOUT")

		if [ "${COUNT}" -ne 0 ]; then
			OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT id, set, COUNT(set) FROM fortune_bag GROUP BY id, set HAVING COUNT(set) >= 1), t2 AS (SELECT id, SUM(CASE WHEN set <= 100 THEN 1 ELSE 0 END) AS f1, SUM(CASE WHEN set > 100 THEN 1 ELSE 0 END) AS f3 FROM t1 GROUP BY id) SELECT * FROM t1 WHERE id IN (SELECT id FROM t2 WHERE f1 > 0 AND f3 > 0)) TO '${output_filename}' CSV HEADER")
			send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 一般 <=> 自選 衝突"
		fi

		## 新增規則2
		COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH tb AS (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set > 110 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) SELECT COUNT(*) FROM tb) TO STDOUT")

		if [ "${COUNT}" -ne 0 ]; then
			OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (SELECT id, set, COUNT(set) FROM fortune_bag WHERE set BETWEEN > 110 GROUP BY id, set HAVING COUNT(set) > 1 ORDER BY id, set) TO '${output_filename}' CSV HEADER")
			send_messages ERROR fb_probability "${ENV_TAG} 福袋表道具 掉落區間 設定異常 ( set > 110 )"
		fi

		break
	fi
done
