from flask import request, Flask, jsonify
from pv.pv_inference import load_model, pv_forecast
from logzero import logger

app = Flask(__name__)
gbm_pv = load_model('./pv/models/pv_pred.joblib')
# todo: 写一个flask接口

@app.route('/pv', methods=['POST'])
def run_pv_forecast():
    """todo: 需要测试

    Returns:
        _type_: _description_
    """
    data = request.data
    if not data or 'inputs' not in data:
        return jsonify({"error": "Invalid data"}), 400
    else:
        # todo: 这里需要写个判断inputs是否合规的逻辑
        inputs = data.get('inputs').reshape(1, 24)
        logger.info(f"pv history inputs: {inputs}")
        out = pv_forecast(inputs, gbm_pv)
        results = {"result": out}
        return jsonify(results), 200
    
@app.route('/carbon', methods=['POST'])
def run_carbon_forecast():
    """
    todo: 封装其他的预测
    """
    pass

if __name__=='__main__':
    app.run(host='0.0.0.0', port='2467')