from app.api import weather
from flask import Flask
import os
"""
-------------------------------------------------------------------------
File: __init__.py
Description: 
Author: 소지안 프로
Created: 2026-02-02
Last Modified: 2026-02-02
-------------------------------------------------------------------------
"""

def create_app():
    app = Flask(__name__)

    # 설정
    app.config['SECRET_KEY'] = 'your-secret-key-here'

    # 라우트 등록
    from app.routes import main
    app.register_blueprint(main.bp)

    # API 블루프린트 등록
    from app.api import water_body_segmentation, flood, drought, vulnerability, terrain
    app.register_blueprint(water_body_segmentation.bp)
    app.register_blueprint(flood.bp)
    app.register_blueprint(drought.bp)
    app.register_blueprint(vulnerability.bp)
    app.register_blueprint(terrain.bp)
    app.register_blueprint(weather.bp)

    # 기상청 예보: 매일 밤 12시(KST)에 API 호출하여 캐시 갱신
    if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        from apscheduler.schedulers.background import BackgroundScheduler
        scheduler = BackgroundScheduler(timezone='Asia/Seoul')
        scheduler.add_job(
            weather.fetch_and_cache_forecast,
            'cron',
            hour=0,
            minute=0,
            id='kma_midnight_fetch'
        )
        scheduler.start()

    return app
