from flask import Flask
from flask_restx import Api
from auth import Auth
from action import Action
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from tools.weather_agency_api.weather_api import update_weather_info_to_today
from tools.algo.SARIMA import sarima

app = Flask(__name__)

print("Api Start")
api = Api(    app,
    version='0.1',
    title="monitoring",
    description="API Server",
    terms_url="/",
    contact="dhlim@takensoft.co.kr",
    )


scheduler = BackgroundScheduler()
scheduler.start()
today = datetime.today().strftime('%Y-%m-%d')
# Schedule task_function to be called every 1 hour
scheduler.add_job(
    func=update_weather_info_to_today,
    trigger=IntervalTrigger(hours=1),
    id='weather_data_update',
    name='update weather time every 6 hours',
    replace_existing=True
)
scheduler.add_job(
    func=sarima,
    trigger=IntervalTrigger(hours=1),
    args=("data/weather/weather_data.csv", f"{today}"),
    # comma to make it a tuple, so that python won't confuse this as a list of char
    id='weather_data_update',
    name='update weather time every 6 hours',
    replace_existing=True
)

api.add_namespace(Action, '/action')
# update_weather_info_to_today("data/weather/weather_data.csv")
# sarima("data/weather/weather_data.csv",f"{today}")

api.add_namespace(Auth, '/auth')
print("Api Add Auth")


if __name__ == "__main__":
    try:
        app.run(debug=False, host='0.0.0.0', port=8080)
    except:
        scheduler.shutdown()
    print("Flask Start")