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

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

env=$1
output_filename="/tmp/online_gacha_grab_bag_vs_powerupitem_item_id_monitor.log"

COUNT1=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT id AS item_id FROM online_gacha_grab_bag WHERE id NOT IN (SELECT item_id FROM powerupitem) ORDER BY id) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT2=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT item_id FROM online_gacha_grab_bag WHERE item_id NOT IN (SELECT item_id FROM powerupitem) ORDER BY item_id) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT3=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT bonus_bag FROM online_gacha_grab_bag WHERE bonus_bag <> 0 AND bonus_bag NOT IN (SELECT item_id FROM powerupitem) ORDER BY bonus_bag) SELECT COUNT(bonus_bag) FROM t1) TO STDOUT;")

if [ "${COUNT1}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT id AS item_id FROM online_gacha_grab_bag WHERE id NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY item_id) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR online_gacha_grab_bag_vs_powerupitem_item_id "${ENV_TAG} 在線免費福袋表的福袋道具ID 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT2}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT item_id FROM online_gacha_grab_bag WHERE item_id NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY item_id) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR online_gacha_grab_bag_vs_powerupitem_item_id "${ENV_TAG} 在線免費福袋表的內容物道具ID 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT3}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT bonus_bag FROM online_gacha_grab_bag WHERE bonus_bag <> 0 AND bonus_bag NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY bonus_bag) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR online_gacha_grab_bag_vs_powerupitem_item_id "${ENV_TAG} 在線免費福袋表的獎勵福袋道具ID 設定版本內未包含的道具ID品項"
fi
