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

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

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

COUNT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT week_type, theme_type, item_id1, item_id2, item_id3, item_id4, item_id5, item_id6, item_id7, item_id8 FROM starway WHERE (item_id1 NOT IN (SELECT item_id FROM powerupitem) AND (item_id1 <> 0) ) OR ( item_id2 NOT IN (SELECT item_id FROM powerupitem) AND (item_id2 <> 0) ) OR ( item_id3 NOT IN (SELECT item_id FROM powerupitem) AND (item_id3 <> 0) ) OR ( item_id4 NOT IN (SELECT item_id FROM powerupitem) AND (item_id4 <> 0) ) OR ( item_id5 NOT IN (SELECT item_id FROM powerupitem) AND (item_id5 <> 0) ) OR ( item_id6 NOT IN (SELECT item_id FROM powerupitem) AND (item_id6 <> 0) ) OR ( item_id7 NOT IN (SELECT item_id FROM powerupitem) AND (item_id7 <> 0) ) OR ( item_id8 NOT IN (SELECT item_id FROM powerupitem) AND (item_id8 <> 0) ) ORDER BY week_type, theme_type) SELECT COUNT(*) FROM t1) TO STDOUT;")

if [ "${COUNT}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT week_type, theme_type, item_id1, item_id2, item_id3, item_id4, item_id5, item_id6, item_id7, item_id8 FROM starway WHERE (item_id1 NOT IN (SELECT item_id FROM powerupitem) AND (item_id1 <> 0) ) OR ( item_id2 NOT IN (SELECT item_id FROM powerupitem) AND (item_id2 <> 0) ) OR ( item_id3 NOT IN (SELECT item_id FROM powerupitem) AND (item_id3 <> 0) ) OR ( item_id4 NOT IN (SELECT item_id FROM powerupitem) AND (item_id4 <> 0) ) OR ( item_id5 NOT IN (SELECT item_id FROM powerupitem) AND (item_id5 <> 0) ) OR ( item_id6 NOT IN (SELECT item_id FROM powerupitem) AND (item_id6 <> 0) ) OR ( item_id7 NOT IN (SELECT item_id FROM powerupitem) AND (item_id7 <> 0) ) OR ( item_id8 NOT IN (SELECT item_id FROM powerupitem) AND (item_id8 <> 0) ) ORDER BY week_type, theme_type) SELECT week_type, theme_type, CASE WHEN item_id1 > 0 AND item_id1 NOT IN (SELECT item_id FROM powerupitem) THEN item_id1 ELSE 1 END AS item_id1, CASE WHEN item_id2 > 0 AND item_id2 NOT IN (SELECT item_id FROM powerupitem) THEN item_id2 ELSE 1 END AS item_id2, CASE WHEN item_id3 > 0 AND item_id3 NOT IN (SELECT item_id FROM powerupitem) THEN item_id3 ELSE 1 END AS item_id3, CASE WHEN item_id4 > 0 AND item_id4 NOT IN (SELECT item_id FROM powerupitem) THEN item_id4 ELSE 1 END AS item_id4, CASE WHEN item_id5 > 0 AND item_id5 NOT IN (SELECT item_id FROM powerupitem) THEN item_id5 ELSE 1 END AS item_id5, CASE WHEN item_id6 > 0 AND item_id6 NOT IN (SELECT item_id FROM powerupitem) THEN item_id6 ELSE 1 END AS item_id6, CASE WHEN item_id7 > 0 AND item_id7 NOT IN (SELECT item_id FROM powerupitem) THEN item_id7 ELSE 1 END AS item_id7, CASE WHEN item_id8 > 0 AND item_id8 NOT IN (SELECT item_id FROM powerupitem) THEN item_id8 ELSE 1 END AS item_id8 FROM t1 ORDER BY week_type, theme_type) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR starway_vs_powerupitem_item_id "${ENV_TAG} 星光表上設定版本內未包含的道具ID品項"
fi
