 import uvicorn
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
import importlib
import sys
import os

app = FastAPI()

# 1. Allow your HTML to talk to this server
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], 
    allow_methods=["*"],
    allow_headers=["*"],
)

# 2. Define a path to your existing scriptsimport os

# 3. Dynamically import your existing script
# Replace 'my_unconnected_script' with the name of your Python file (without .py)
# e.g., if your file is 'workflow_generator.py', use 'workflow_generator'
script_name = "my_unconnected_script"
module = importlib.import_module(script_name)

# 4. Expose a function from your script as an API endpoint
# Assume your script has a function called 'run_workflow' that takes a prompt
@app.post("/run-workflow")
async def run_workflow_endpoint(prompt: str):
    # This calls the function from your existing Python script
    result = module.run_workflow(prompt)
    return {"output": result}

# 5. Serve your existing HTML file at the root
@app.get("/")
async def serve_html():
    # This serves your existing HTML file. Replace 'index.html' with your actual filename.
    return FileResponse('index.html')

# 6. If you have other functions, expose them similarly
# For example, if your script has a function 'get_status()':
# @app.get("/status")
# async def get_status_endpoint():
#     status = module.get_status()
#     return {"status": status}

if __name__ == "__main__":
    # Run the server on port 8000 (or any free port)
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=False)