import datetime
import json

import pandas as pd
import requests

symbols = ['ACWI', 'EWJ', 'MCHI', 'EEM', 'BKF', 'INDA', 'AAXJ', 'VGK', 'QQQ', 'SPY', 'SPX', 'IWN',
           'IUSG', 'IWD', 'DON', 'GDX', 'TOLZ', 'XLU', 'XBI', 'ESGD', 'IGE', 'EMLC', 'IGAA',
           'LQD', 'HYG', 'SHY', 'IEI', 'IEF', 'GLD', 'IYR', 'UUP', 'CEW', 'TLT']


def do_reporter(start='2023-10-01', end=datetime.date.today()):
    url = f"https://jrp.jfquant.com/api/v1.0/ai/predict?startTime={start}&endTime={end.strftime('%Y-%m-%d')}"
    resp = requests.get(url)
    datas = []
    symbol_index_dict = {symbol: index for index, symbol in enumerate(symbols)}
    for value in resp.json()['body'].values():
        for item in value:
            data = {
                'Forcast On Date': item['aiPredict']['predictDate'],
                'Ticker': item['bloombergTicker'].replace(' Index', '').replace(' Equity', ''),
                'In 21 business days': 'UP' if item['aiPredict']['predict'] == 1 else 'DOWN',
                'Ticker Name': item['indexName'],
            }
            datas.append(data)
    sorted_data = sorted(datas, key=lambda x: symbol_index_dict[x['Ticker'].split(' ')[0]])
    print(json.dumps(sorted_data, ensure_ascii=False))
    pf = pd.DataFrame(sorted_data)
    pf.to_excel("Forcast_Report.xlsx", index=False)


if __name__ == '__main__':
    do_reporter()