
import functions
import config

while True:

	# ------------------------------------------- #
	# PART 1: Updating the notion trades database #
	# ------------------------------------------- #
	# Fetches the list of all trades stored in notion
	trades = functions.fetch_format_notion_trades(config.notion_db_id_trades)

	# Generates a list with unique tickers and no duplicates to reduce workload for the yfinance api
	tickers = functions.filter_list_of_tickers(trades)

	# Fetches & formats the complete history per ticker from yfinance
	yf_data = functions.fetch_format_yf_data(tickers)

	# Calculates & stores a history per trade
	history_per_trade = functions.calc_history_per_trade(trades, yf_data)

	# Selects the most current values from the history per trade and overwrites them in the "trades" feteched from notion
	trades = functions.select_current_value_per_trade(trades, history_per_trade)

	# Updates the values in the notion database
	functions.push_notion_trades_update(trades)


	# ------------------------------------------------ #
	# PART 2: Updating the notion investments database #
	# ------------------------------------------------ #
	# Fetches the list of entries in the investment-overview database stored in notion
	investments = functions.fetch_format_notion_investments(config.notion_db_id_investments)

	# Calculates & stores a history per ticker AND a total across all tickers indexed by the ticker name
	history_per_ticker = functions.calc_history_per_ticker(history_per_trade, tickers, trades)

	# Selects the most current values from the history per ticker and overwrites them in the "investments" feteched from notion
	investments = functions.select_current_value_per_ticker(investments, history_per_ticker)

	# Updates the values in the notion database
	functions.push_notion_investment_update(investments)


	# --------------------------------- #
	# PART 3: Updating the TRMNL Screen #
	# --------------------------------- #
	# Creates a list containing one date per week
	list_wkl_dates = functions.create_list_wkl_dates(trades)

	# Filter a weekly snapshot from the history per ticker
	history_per_ticker_wkl = functions.filter_history_by_list(history_per_ticker, list_wkl_dates)

	# Create a screen for the overall performance
	# ......

	# Push the weekly screen to TRML
	# functions.push_trmnl_update(test_data, trmnl_id)



	# --------------------------- #
	# PART 4: Cool off and repeat #
	# --------------------------- #
	# Logging
	print("[INFO] Completed cycle at: {}".format(functions.datetime.datetime.now()))
	print("[INFO] Waiting a few minutes before the next execution")
	print("---------------------------------------------------------------------------")

	# Clear variables
	trades = {}
	yf_data = {}
	history_per_trade = {}
	tickers = []

	# Wait for api-cooldown
	functions.time.sleep(config.programm_cooldown_time * 60)