from flask import Flask, request, jsonify, send_file
import os
from chatbot import get_response

app = Flask(__name__)

@app.route('/astrology', methods=['POST'])
def generate_report():
    # Extract data from form data
    name = request.form.get("name")
    year = request.form.get("year")
    month = request.form.get("month")
    day = request.form.get("day")
    hour = request.form.get("hour")
    minute = request.form.get("minute")
    city = request.form.get("city")
    nation = request.form.get("nation")
    
    # Validate input
    if not all([name, year, month, day, hour, minute, city, nation]):
        return jsonify({"error": "All fields are required"}), 400

    # Get the first part of the name up to the first space
    file_name = name.split()[0]
    svg_file_name = f"{file_name} - Natal Chart.svg"

    # Construct the command to run the other Python script
    command = f'python3 planet.py "{name}" {year} {month} {day} {hour} {minute} "{city}" "{nation}" > {file_name}.txt'

    # Execute the command using os.system()
    os.system(command)

    # Get response from chatbot
    response, report = get_response(f"{file_name}.txt")
    
    # Clean up the generated file
    os.remove(f"{file_name}.txt")

    # Return the SVG file path and other data
    return jsonify({"response": response, "svg_file": svg_file_name}), 200


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

