from flask import Flask, request, jsonify
import logging
from cofe_simulation import run_cofe_simulation

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

@app.route('/health', methods=['GET'])
def health_check():
    return jsonify({"status": "healthy", "service": "COFE Simulation API"})

@app.route('/simulate', methods=['POST'])
def simulate():
    try:
        data = request.json
        logging.info(f"Received simulation request: {data}")
        simulation_params = {
            'temperature_range': data.get('temperatureEntree', 25),
            'inlet_pressure': data.get('pressionEntree', 70),
            'mass_flow': data.get('debitMassique', 83.94),
            'composition': data.get('composition', {}),
            'options': data.get('options', {})
        }
        results = run_cofe_simulation(simulation_params)
        return jsonify({
            "success": True,
            "results": results
        })
    except Exception as e:
        logging.error(f"Simulation error: {str(e)}")
        return jsonify({
            "success": False,
            "error": str(e)
        }), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=False)